package mongo

import (
	"context"
	"time"

	"github.com/tech/sendico/billing/fees/storage"
	"github.com/tech/sendico/billing/fees/storage/mongo/store"
	"github.com/tech/sendico/pkg/db"
	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/mlogger"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.uber.org/zap"
)

type Store struct {
	logger mlogger.Logger
	conn   *db.MongoConnection
	db     *mongo.Database

	plans storage.PlansStore
}

// New creates a repository backed by MongoDB for the billing fees service.
func New(logger mlogger.Logger, conn *db.MongoConnection) (*Store, error) {
	if conn == nil {
		return nil, merrors.InvalidArgument("mongo connection is nil")
	}

	client := conn.Client()
	if client == nil {
		return nil, merrors.Internal("mongo client not initialised")
	}

	database := conn.Database()
	result := &Store{
		logger: logger.Named("storage").Named("mongo"),
		conn:   conn,
		db:     database,
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := result.Ping(ctx); err != nil {
		result.logger.Error("Mongo ping failed during store init", zap.Error(err))

		return nil, err
	}

	plansStore, err := store.NewPlans(result.logger, database)
	if err != nil {
		result.logger.Error("Failed to initialise plans store", zap.Error(err))

		return nil, err
	}

	result.plans = plansStore

	result.logger.Info("Billing fees MongoDB storage initialised")

	return result, nil
}

func (s *Store) Ping(ctx context.Context) error {
	return s.conn.Ping(ctx)
}

func (s *Store) Plans() storage.PlansStore {
	return s.plans
}

var _ storage.Repository = (*Store)(nil)
