package gateway

import (
	"testing"
	"time"

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

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

func TestValidateCardTokenizeRequest_ValidNestedCard(t *testing.T) {
	cfg := testMonetixConfig()
	req := validCardTokenizeRequest()
	req.Card = &mntxv1.CardDetails{
		Pan:        "4111111111111111",
		ExpMonth:   req.CardExpMonth,
		ExpYear:    req.CardExpYear,
		CardHolder: req.CardHolder,
		Cvv:        req.CardCvv,
	}
	req.CardPan = ""
	req.CardExpMonth = 0
	req.CardExpYear = 0
	req.CardHolder = ""
	req.CardCvv = ""

	if _, err := validateCardTokenizeRequest(req, cfg); err != nil {
		t.Fatalf("expected no error, got %v", err)
	}
}

func TestValidateCardTokenizeRequest_Expired(t *testing.T) {
	cfg := testMonetixConfig()
	req := validCardTokenizeRequest()
	now := time.Now().UTC()
	req.CardExpMonth = uint32(now.Month())   //nolint:gosec // month value is bounded by time.Time
	req.CardExpYear = uint32(now.Year() - 1) //nolint:gosec // test value intentionally uses previous year

	_, err := validateCardTokenizeRequest(req, cfg)
	requireReason(t, err, "expired_card")
}

func TestValidateCardTokenizeRequest_MissingCvv(t *testing.T) {
	cfg := testMonetixConfig()
	req := validCardTokenizeRequest()
	req.CardCvv = ""

	_, err := validateCardTokenizeRequest(req, cfg)
	requireReason(t, err, "missing_cvv")
}

func TestValidateCardTokenizeRequest_MissingCardPan(t *testing.T) {
	cfg := testMonetixConfig()
	req := validCardTokenizeRequest()
	req.CardPan = ""

	_, err := validateCardTokenizeRequest(req, cfg)
	requireReason(t, err, "missing_card_pan")
}

func TestValidateCardTokenizeRequest_AddressRequired(t *testing.T) {
	cfg := testMonetixConfig()
	cfg.RequireCustomerAddress = true
	req := validCardTokenizeRequest()
	req.CustomerCountry = ""

	_, err := validateCardTokenizeRequest(req, cfg)
	requireReason(t, err, "missing_customer_country")
}
