package storage

import (
	"context"
	"errors"

	"github.com/tech/sendico/payments/storage/model"
	quotestorage "github.com/tech/sendico/payments/storage/quote"
	pkgmodel "github.com/tech/sendico/pkg/model"
	"go.mongodb.org/mongo-driver/v2/bson"
)

var (
	// ErrPaymentNotFound signals that a payment record does not exist.
	ErrPaymentNotFound = errors.New("payments.storage: payment not found")
	// ErrDuplicatePayment signals that idempotency constraints were violated.
	ErrDuplicatePayment = errors.New("payments.storage: duplicate payment")
	// ErrRouteNotFound signals that a payment route record does not exist.
	ErrRouteNotFound = errors.New("payments.storage: route not found")
	// ErrDuplicateRoute signals that a route already exists for the same transition.
	ErrDuplicateRoute = errors.New("payments.storage: duplicate route")
)

// Repository exposes persistence primitives for the payments domain.
type Repository interface {
	Ping(ctx context.Context) error
	Payments() PaymentsStore
	PaymentMethods() PaymentMethodsStore
	Quotes() quotestorage.QuotesStore
	Routes() RoutesStore
}

// PaymentsStore manages payment lifecycle state.
type PaymentsStore interface {
	Create(ctx context.Context, payment *model.Payment) error
	Update(ctx context.Context, payment *model.Payment) error
	GetByPaymentRef(ctx context.Context, paymentRef string) (*model.Payment, error)
	GetByIdempotencyKey(ctx context.Context, orgRef bson.ObjectID, idempotencyKey string) (*model.Payment, error)
	GetByChainTransferRef(ctx context.Context, transferRef string) (*model.Payment, error)
	List(ctx context.Context, filter *model.PaymentFilter) (*model.PaymentList, error)
}

// PaymentMethodsStore manages recipient-linked payment methods.
type PaymentMethodsStore interface {
	Create(ctx context.Context, accountRef, organizationRef bson.ObjectID, method *pkgmodel.PaymentMethod) error
	Get(ctx context.Context, accountRef, methodRef bson.ObjectID) (*pkgmodel.PaymentMethod, error)
	GetPrivate(ctx context.Context, methodRef bson.ObjectID) (*pkgmodel.PaymentMethod, error)
	Update(ctx context.Context, accountRef bson.ObjectID, method *pkgmodel.PaymentMethod) error
	Delete(ctx context.Context, accountRef, methodRef bson.ObjectID) error
	DeleteCascade(ctx context.Context, accountRef, methodRef bson.ObjectID) error
	SetArchived(ctx context.Context, accountRef, organizationRef, methodRef bson.ObjectID, archived, cascade bool) error
	List(ctx context.Context, accountRef, organizationRef, recipientRef bson.ObjectID, cursor *pkgmodel.ViewCursor) ([]pkgmodel.PaymentMethod, error)
	ListPrivate(ctx context.Context, organizationRef, recipientRef bson.ObjectID, cursor *pkgmodel.ViewCursor) ([]pkgmodel.PaymentMethod, error)

	SetArchivedByRecipient(ctx context.Context, recipientRef bson.ObjectID, archived bool) (int, error)
	DeleteByRecipient(ctx context.Context, recipientRef bson.ObjectID) error
}

// RoutesStore manages allowed routing transitions.
type RoutesStore interface {
	Create(ctx context.Context, route *model.PaymentRoute) error
	Update(ctx context.Context, route *model.PaymentRoute) error
	GetByID(ctx context.Context, id bson.ObjectID) (*model.PaymentRoute, error)
	List(ctx context.Context, filter *model.PaymentRouteFilter) (*model.PaymentRouteList, error)
}
