package storage

import (
	"context"

	"github.com/tech/sendico/payments/orchestrator/storage/model"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

type storageError string

func (e storageError) Error() string {
	return string(e)
}

var (
	// ErrPaymentNotFound signals that a payment record does not exist.
	ErrPaymentNotFound = storageError("payments.orchestrator.storage: payment not found")
	// ErrDuplicatePayment signals that idempotency constraints were violated.
	ErrDuplicatePayment = storageError("payments.orchestrator.storage: duplicate payment")
	// ErrQuoteNotFound signals that a stored quote does not exist or expired.
	ErrQuoteNotFound = storageError("payments.orchestrator.storage: quote not found")
	// ErrDuplicateQuote signals that a quote reference already exists.
	ErrDuplicateQuote = storageError("payments.orchestrator.storage: duplicate quote")
	// ErrRouteNotFound signals that a payment route record does not exist.
	ErrRouteNotFound = storageError("payments.orchestrator.storage: route not found")
	// ErrDuplicateRoute signals that a route already exists for the same transition.
	ErrDuplicateRoute = storageError("payments.orchestrator.storage: duplicate route")
	// ErrPlanTemplateNotFound signals that a plan template record does not exist.
	ErrPlanTemplateNotFound = storageError("payments.orchestrator.storage: plan template not found")
	// ErrDuplicatePlanTemplate signals that a plan template already exists for the same transition.
	ErrDuplicatePlanTemplate = storageError("payments.orchestrator.storage: duplicate plan template")
)

// Repository exposes persistence primitives for the orchestrator domain.
type Repository interface {
	Ping(ctx context.Context) error
	Payments() PaymentsStore
	Quotes() QuotesStore
	Routes() RoutesStore
	PlanTemplates() PlanTemplatesStore
}

// 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 primitive.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)
}

// QuotesStore manages temporary stored payment quotes.
type QuotesStore interface {
	Create(ctx context.Context, quote *model.PaymentQuoteRecord) error
	GetByRef(ctx context.Context, orgRef primitive.ObjectID, quoteRef string) (*model.PaymentQuoteRecord, error)
	GetByIdempotencyKey(ctx context.Context, idempotencyKey string) (*model.PaymentQuoteRecord, 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 primitive.ObjectID) (*model.PaymentRoute, error)
	List(ctx context.Context, filter *model.PaymentRouteFilter) (*model.PaymentRouteList, error)
}

// PlanTemplatesStore manages orchestration plan templates.
type PlanTemplatesStore interface {
	Create(ctx context.Context, template *model.PaymentPlanTemplate) error
	Update(ctx context.Context, template *model.PaymentPlanTemplate) error
	GetByID(ctx context.Context, id primitive.ObjectID) (*model.PaymentPlanTemplate, error)
	List(ctx context.Context, filter *model.PaymentPlanTemplateFilter) (*model.PaymentPlanTemplateList, error)
}
