package store

import (
	"context"
	"strings"

	storage "github.com/tech/sendico/gateway/aurora/storage"
	"github.com/tech/sendico/gateway/aurora/storage/model"
	"github.com/tech/sendico/pkg/db/repository"
	ri "github.com/tech/sendico/pkg/db/repository/index"
	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/mutil/mzap"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.uber.org/zap"
)

const (
	payoutsCollection = "card_payouts"
	payoutIdemField   = "idempotencyKey"
	payoutIdField     = "paymentRef"
	payoutOpField     = "operationRef"
)

type Payouts struct {
	logger     mlogger.Logger
	repository repository.Repository
}

func NewPayouts(logger mlogger.Logger, db *mongo.Database) (*Payouts, error) {
	if db == nil {
		return nil, merrors.InvalidArgument("mongo database is nil")
	}
	if logger == nil {
		logger = zap.NewNop()
	}
	logger = logger.Named("payouts").With(zap.String("collection", payoutsCollection))

	repo := repository.CreateMongoRepository(db, payoutsCollection)
	if err := repo.CreateIndex(&ri.Definition{
		Keys:   []ri.Key{{Field: payoutOpField, Sort: ri.Asc}},
		Unique: true,
		Sparse: true,
	}); err != nil {
		logger.Error("Failed to create payouts operation index",
			zap.Error(err), zap.String("index_field", payoutOpField))
		return nil, err
	}
	if err := repo.CreateIndex(&ri.Definition{
		Keys:   []ri.Key{{Field: payoutIdemField, Sort: ri.Asc}},
		Unique: true,
	}); err != nil {
		logger.Error("Failed to create payouts idempotency index",
			zap.Error(err), zap.String("index_field", payoutIdemField))
		return nil, err
	}

	p := &Payouts{
		logger:     logger,
		repository: repo,
	}
	p.logger.Debug("Payouts store initialised")
	return p, nil
}

func (p *Payouts) findOneByField(ctx context.Context, field, value string) (*model.CardPayout, error) {
	var res model.CardPayout
	return &res, p.repository.FindOneByFilter(ctx, repository.Filter(field, value), &res)
}

func (p *Payouts) FindByIdempotencyKey(ctx context.Context, key string) (*model.CardPayout, error) {
	return p.findOneByField(ctx, payoutIdemField, key)
}

func (p *Payouts) FindByOperationRef(ctx context.Context, operationRef string) (*model.CardPayout, error) {
	return p.findOneByField(ctx, payoutOpField, operationRef)
}

func (p *Payouts) FindByPaymentID(ctx context.Context, paymentID string) (*model.CardPayout, error) {
	return p.findOneByField(ctx, payoutIdField, paymentID)
}

func (p *Payouts) Upsert(ctx context.Context, record *model.CardPayout) error {
	if record == nil {
		p.logger.Warn("Invalid argument provided: nil record")
		return merrors.InvalidArgument("payout record is nil", "record")
	}

	record.OperationRef = strings.TrimSpace(record.OperationRef)
	record.PaymentRef = strings.TrimSpace(record.PaymentRef)
	record.CustomerID = strings.TrimSpace(record.CustomerID)
	record.Comment = strings.TrimSpace(record.Comment)
	record.ProviderCode = strings.TrimSpace(record.ProviderCode)
	record.ProviderPaymentID = strings.TrimSpace(record.ProviderPaymentID)

	if record.OperationRef == "" {
		p.logger.Warn("Invalid argument provided: operation reference missing")
		return merrors.InvalidArgument("operation ref is required", "operation_ref")
	}

	if err := p.repository.Upsert(ctx, record); err != nil {
		p.logger.Warn("Failed to upsert payout record", zap.Error(err), mzap.ObjRef("payout_ref", record.ID),
			zap.String("operation_ref", record.OperationRef), zap.String("payment_ref", record.PaymentRef))
		return err
	}
	return nil
}

var _ storage.PayoutsStore = (*Payouts)(nil)
