package srequest

import (
	"testing"

	paymenttypes "github.com/tech/sendico/pkg/payments/types"
)

func TestPaymentIntentValidate_AcceptsBaseIntentWithoutFX(t *testing.T) {
	intent := mustValidBaseIntent(t)

	if err := intent.Validate(); err != nil {
		t.Fatalf("unexpected validation error: %v", err)
	}
}

func TestPaymentIntentValidate_RejectsFXWithoutPair(t *testing.T) {
	intent := mustValidBaseIntent(t)
	intent.FX = &FXIntent{
		Side: FXSideSellBaseBuyQuote,
	}

	if err := intent.Validate(); err == nil {
		t.Fatalf("expected validation error for missing fx pair")
	}
}

func TestPaymentIntentValidate_RejectsInvalidFXSide(t *testing.T) {
	intent := mustValidBaseIntent(t)
	intent.FX = &FXIntent{
		Pair: &CurrencyPair{
			Base:  "USDT",
			Quote: "RUB",
		},
		Side: FXSide("wrong"),
	}

	if err := intent.Validate(); err == nil {
		t.Fatalf("expected validation error for invalid fx side")
	}
}

func TestPaymentIntentValidate_AcceptsValidFX(t *testing.T) {
	intent := mustValidBaseIntent(t)
	intent.FX = &FXIntent{
		Pair: &CurrencyPair{
			Base:  "USDT",
			Quote: "RUB",
		},
		Side: FXSideSellBaseBuyQuote,
	}

	if err := intent.Validate(); err != nil {
		t.Fatalf("unexpected validation error: %v", err)
	}
}

func mustValidBaseIntent(t *testing.T) *PaymentIntent {
	t.Helper()

	source, err := NewManagedWalletEndpointDTO(ManagedWalletEndpoint{ManagedWalletRef: "mw-src"}, nil)
	if err != nil {
		t.Fatalf("build source endpoint: %v", err)
	}
	destination, err := NewCardEndpointDTO(CardEndpoint{
		Pan:       "2200700142860161",
		FirstName: "Jane",
		LastName:  "Doe",
		ExpMonth:  2,
		ExpYear:   2030,
	}, nil)
	if err != nil {
		t.Fatalf("build destination endpoint: %v", err)
	}

	return &PaymentIntent{
		Kind:           PaymentKindPayout,
		Source:         &source,
		Destination:    &destination,
		Amount:         &paymenttypes.Money{Amount: "10", Currency: "USDT"},
		SettlementMode: SettlementModeFixSource,
		FeeTreatment:   FeeTreatmentAddToSource,
	}
}
