package gateway

import (
	"testing"

	"github.com/tech/sendico/gateway/mntx/internal/service/monetix"
	mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
)

func TestValidateCardPayoutRequest_Valid(t *testing.T) {
	cfg := testMonetixConfig()
	req := validCardPayoutRequest()
	if err := validateCardPayoutRequest(req, cfg); err != nil {
		t.Fatalf("expected no error, got %v", err)
	}
}

func TestValidateCardPayoutRequest_Errors(t *testing.T) {
	baseCfg := testMonetixConfig()
	cases := []struct {
		name     string
		mutate   func(*mntxv1.CardPayoutRequest)
		config   func(monetix.Config) monetix.Config
		expected string
	}{
		{
			name:     "missing_payout_id",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.PayoutId = "" },
			expected: "missing_payout_id",
		},
		{
			name:     "missing_customer_id",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.CustomerId = "" },
			expected: "missing_customer_id",
		},
		{
			name:     "missing_customer_ip",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.CustomerIp = "" },
			expected: "missing_customer_ip",
		},
		{
			name:     "invalid_amount",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.AmountMinor = 0 },
			expected: "invalid_amount",
		},
		{
			name:     "missing_currency",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.Currency = "" },
			expected: "missing_currency",
		},
		{
			name:   "unsupported_currency",
			mutate: func(r *mntxv1.CardPayoutRequest) { r.Currency = "EUR" },
			config: func(cfg monetix.Config) monetix.Config {
				cfg.AllowedCurrencies = []string{"USD"}
				return cfg
			},
			expected: "unsupported_currency",
		},
		{
			name:     "missing_card_pan",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.CardPan = "" },
			expected: "missing_card_pan",
		},
		{
			name:     "missing_card_holder",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.CardHolder = "" },
			expected: "missing_card_holder",
		},
		{
			name:     "invalid_expiry_month",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.CardExpMonth = 13 },
			expected: "invalid_expiry_month",
		},
		{
			name:     "invalid_expiry_year",
			mutate:   func(r *mntxv1.CardPayoutRequest) { r.CardExpYear = 0 },
			expected: "invalid_expiry_year",
		},
		{
			name:   "missing_customer_country_when_required",
			mutate: func(r *mntxv1.CardPayoutRequest) { r.CustomerCountry = "" },
			config: func(cfg monetix.Config) monetix.Config {
				cfg.RequireCustomerAddress = true
				return cfg
			},
			expected: "missing_customer_country",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			req := validCardPayoutRequest()
			tc.mutate(req)
			cfg := baseCfg
			if tc.config != nil {
				cfg = tc.config(cfg)
			}
			err := validateCardPayoutRequest(req, cfg)
			requireReason(t, err, tc.expected)
		})
	}
}
