package quote_computation_service

import (
	"time"

	"github.com/tech/sendico/payments/quotation/internal/service/quotation/gateway_funding_profile"
	"github.com/tech/sendico/payments/storage/model"
	"github.com/tech/sendico/pkg/model/account_role"
	feesv1 "github.com/tech/sendico/pkg/proto/billing/fees/v1"
	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
	paymentv1 "github.com/tech/sendico/pkg/proto/common/payment/v1"
	oraclev1 "github.com/tech/sendico/pkg/proto/oracle/v1"
	quotationv2 "github.com/tech/sendico/pkg/proto/payments/quotation/v2"
	"go.mongodb.org/mongo-driver/v2/bson"
)

// PlanMode defines whether the computation plan is for one intent or many.
type PlanMode string

const (
	PlanModeUnspecified PlanMode = "unspecified"
	PlanModeSingle      PlanMode = "single"
	PlanModeBatch       PlanMode = "batch"
)

// BuildQuoteInput is the domain input for one quote computation request.
type BuildQuoteInput struct {
	OrganizationRef     string
	IdempotencyKey      string
	PreviewOnly         bool
	Intent              model.PaymentIntent
	Route               *quotationv2.RouteSpecification
	ExecutionConditions *quotationv2.ExecutionConditions
}

// ComputedQuote is a canonical quote payload for v2 processing.
type ComputedQuote struct {
	QuoteRef               string
	DebitAmount            *moneyv1.Money
	CreditAmount           *moneyv1.Money
	TotalCost              *moneyv1.Money
	FeeLines               []*feesv1.DerivedPostingLine
	FeeRules               []*feesv1.AppliedRule
	FXQuote                *oraclev1.Quote
	Route                  *quotationv2.RouteSpecification
	ExecutionConditions    *quotationv2.ExecutionConditions
	ResolvedSettlementMode paymentv1.SettlementMode
	ResolvedFeeTreatment   quotationv2.FeeTreatment
}

// QuoteComputationPlan is an orchestration plan for quote computations.
// It is intentionally separate from executable payment plans.
type QuoteComputationPlan struct {
	Mode               PlanMode
	OrganizationRef    string
	OrganizationID     bson.ObjectID
	PreviewOnly        bool
	BaseIdempotencyKey string
	Items              []*QuoteComputationPlanItem
}

// QuoteComputationPlanItem is one quote-computation unit.
type QuoteComputationPlanItem struct {
	Index                  int
	IdempotencyKey         string
	IntentRef              string
	Intent                 model.PaymentIntent
	QuoteInput             BuildQuoteInput
	Steps                  []*QuoteComputationStep
	Funding                *gateway_funding_profile.QuoteFundingGate
	Route                  *quotationv2.RouteSpecification
	ExecutionConditions    *quotationv2.ExecutionConditions
	ResolvedSettlementMode paymentv1.SettlementMode
	ResolvedFeeTreatment   quotationv2.FeeTreatment
	BlockReason            quotationv2.QuoteBlockReason
}

// QuoteComputationStep is one planner step in a generic execution graph.
// The planner should use rail+operation instead of custom per-case leg kinds.
type QuoteComputationStep struct {
	StepID             string
	Rail               model.Rail
	Operation          model.RailOperation
	GatewayID          string
	InstanceID         string
	GatewayInvokeURI   string
	DependsOn          []string
	Amount             *moneyv1.Money
	FromRole           *account_role.AccountRole
	ToRole             *account_role.AccountRole
	Optional           bool
	IncludeInAggregate bool
}

// QuoteComputationResult is the computed output for one planned item.
type QuoteComputationResult struct {
	ItemIndex   int
	Quote       *ComputedQuote
	ExpiresAt   time.Time
	BlockReason quotationv2.QuoteBlockReason
}
