package opagg

import (
	"github.com/shopspring/decimal"
	"github.com/tech/sendico/payments/storage/model"
	paymenttypes "github.com/tech/sendico/pkg/payments/types"
)

func sampleIntent(ref, cardToken, amount string) model.PaymentIntent {
	return model.PaymentIntent{
		Ref:  ref,
		Kind: model.PaymentKindPayout,
		Source: model.PaymentEndpoint{
			Type: model.EndpointTypeManagedWallet,
			ManagedWallet: &model.ManagedWalletEndpoint{
				ManagedWalletRef: "src-wallet-1",
				Asset:            &paymenttypes.Asset{Chain: "TRON", TokenSymbol: "USDT"},
			},
		},
		Destination: model.PaymentEndpoint{
			Type: model.EndpointTypeCard,
			Card: &model.CardEndpoint{
				Token:   cardToken,
				Country: "RU",
			},
		},
		Amount: &paymenttypes.Money{
			Amount:   amount,
			Currency: "USDT",
		},
		SettlementMode:     model.SettlementModeFixSource,
		SettlementCurrency: "RUB",
	}
}

func sampleQuote(quoteRef, debit, settlement, feeTotal string) *model.PaymentQuoteSnapshot {
	fee := "1.5"
	tax := "0.3"
	totalCost := addStrings(debit, feeTotal)

	return &model.PaymentQuoteSnapshot{
		QuoteRef: quoteRef,
		DebitAmount: &paymenttypes.Money{
			Amount:   debit,
			Currency: "USDT",
		},
		ExpectedSettlementAmount: &paymenttypes.Money{
			Amount:   settlement,
			Currency: "RUB",
		},
		ExpectedFeeTotal: &paymenttypes.Money{
			Amount:   feeTotal,
			Currency: "USDT",
		},
		TotalCost: &paymenttypes.Money{
			Amount:   totalCost,
			Currency: "USDT",
		},
		FeeLines: []*paymenttypes.FeeLine{
			{
				LedgerAccountRef: "ledger:fees:usdt",
				Money:            &paymenttypes.Money{Amount: fee, Currency: "USDT"},
				LineType:         paymenttypes.PostingLineTypeFee,
				Side:             paymenttypes.EntrySideDebit,
				Meta:             map[string]string{"component": "platform_fee", "provider": paymenttypes.DefaultCardsGatewayID},
			},
			{
				LedgerAccountRef: "ledger:tax:usdt",
				Money:            &paymenttypes.Money{Amount: tax, Currency: "USDT"},
				LineType:         paymenttypes.PostingLineTypeTax,
				Side:             paymenttypes.EntrySideDebit,
				Meta:             map[string]string{"component": "vat", "provider": paymenttypes.DefaultCardsGatewayID},
			},
		},
		FeeRules: []*paymenttypes.AppliedRule{
			{
				RuleID:      "rule.platform.usdt",
				RuleVersion: "2026-02-01",
				Formula:     "flat(1.50)+tax(0.30)",
				TaxCode:     "VAT",
				TaxRate:     "0.20",
				Parameters:  map[string]string{"country": "RU"},
			},
		},
		FXQuote: &paymenttypes.FXQuote{
			QuoteRef:   "fx-usdt-rub",
			Provider:   "test-oracle",
			RateRef:    "rate-usdt-rub",
			Side:       paymenttypes.FXSideSellBaseBuyQuote,
			Firm:       true,
			Price:      &paymenttypes.Decimal{Value: "91.5"},
			BaseAmount: &paymenttypes.Money{Amount: debit, Currency: "USDT"},
			QuoteAmount: &paymenttypes.Money{
				Amount:   settlement,
				Currency: "RUB",
			},
		},
		Route: &paymenttypes.QuoteRouteSpecification{
			RouteRef:          "rte-recipient-1",
			PricingProfileRef: "fee_profile_1",
			Hops: []*paymenttypes.QuoteRouteHop{
				{Index: 1, Rail: "CRYPTO", Role: paymenttypes.QuoteRouteHopRoleSource},
				{Index: 2, Rail: "LEDGER", Role: paymenttypes.QuoteRouteHopRoleTransit, Gateway: "internal"},
				{Index: 3, Rail: "CARD", Role: paymenttypes.QuoteRouteHopRoleDestination, Gateway: paymenttypes.DefaultCardsGatewayID},
			},
			Settlement: &paymenttypes.QuoteRouteSettlement{
				Model: "fix_source",
				Asset: &paymenttypes.Asset{TokenSymbol: "USDT"},
			},
		},
		ExecutionConditions: &paymenttypes.QuoteExecutionConditions{
			Readiness:                         paymenttypes.QuoteExecutionReadinessLiquidityReady,
			BatchingEligible:                  true,
			LiquidityCheckRequiredAtExecution: true,
			LatencyHint:                       "instant",
			Assumptions:                       []string{"execution_time_liquidity_check"},
		},
	}
}

func addStrings(left, right string) string {
	l, lErr := decimal.NewFromString(left)
	r, rErr := decimal.NewFromString(right)
	if lErr != nil || rErr != nil {
		return ""
	}
	return l.Add(r).String()
}
