package quote_computation_service

import (
	"testing"

	"github.com/tech/sendico/payments/storage/model"
	paymenttypes "github.com/tech/sendico/pkg/payments/types"
	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
)

func TestDestinationStepAmount_UsesSideAwareCurrencyFallback(t *testing.T) {
	tests := []struct {
		name string
		side paymenttypes.FXSide
		want string
	}{
		{
			name: "buy_base_sell_quote uses base settlement currency",
			side: paymenttypes.FXSideBuyBaseSellQuote,
			want: "RUB",
		},
		{
			name: "sell_base_buy_quote uses quote settlement currency",
			side: paymenttypes.FXSideSellBaseBuyQuote,
			want: "USDT",
		},
		{
			name: "unspecified defaults to quote settlement currency",
			side: paymenttypes.FXSideUnspecified,
			want: "USDT",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			intent := model.PaymentIntent{
				RequiresFX: true,
				FX: &model.FXIntent{
					Pair: &paymenttypes.CurrencyPair{
						Base:  "RUB",
						Quote: "USDT",
					},
					Side: tt.side,
				},
			}

			got := destinationStepAmount(intent, &moneyv1.Money{
				Amount:   "100",
				Currency: "EUR",
			})
			if got == nil {
				t.Fatal("expected destination amount")
			}
			if got.GetCurrency() != tt.want {
				t.Fatalf("unexpected destination currency: got=%q want=%q", got.GetCurrency(), tt.want)
			}
		})
	}
}

func TestSourceCurrencyFromFX_RespectsSide(t *testing.T) {
	tests := []struct {
		name string
		side paymenttypes.FXSide
		want string
	}{
		{
			name: "buy_base_sell_quote debits quote currency",
			side: paymenttypes.FXSideBuyBaseSellQuote,
			want: "USDT",
		},
		{
			name: "sell_base_buy_quote debits base currency",
			side: paymenttypes.FXSideSellBaseBuyQuote,
			want: "RUB",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := sourceCurrencyFromFX(&model.FXIntent{
				Pair: &paymenttypes.CurrencyPair{
					Base:  "RUB",
					Quote: "USDT",
				},
				Side: tt.side,
			})
			if got != tt.want {
				t.Fatalf("unexpected source currency: got=%q want=%q", got, tt.want)
			}
		})
	}
}
