package gateway

import (
	"errors"
	"testing"
	"time"

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

func requireReason(t *testing.T, err error, reason string) {
	t.Helper()
	if err == nil {
		t.Fatalf("expected error")
	}
	if !errors.Is(err, merrors.ErrInvalidArg) {
		t.Fatalf("expected invalid argument error, got %v", err)
	}
	var reasoned payoutFailure
	if !errors.As(err, &reasoned) {
		t.Fatalf("expected payout failure reason, got %T", err)
	}
	if reasoned.Reason() != reason {
		t.Fatalf("expected reason %q, got %q", reason, reasoned.Reason())
	}
}

func testProviderConfig() provider.Config {
	return provider.Config{
		AllowedCurrencies: []string{"RUB", "USD"},
	}
}

func validCardPayoutRequest() *mntxv1.CardPayoutRequest {
	return &mntxv1.CardPayoutRequest{
		PayoutId:          "payout-1",
		ParentPaymentRef:  "payment-parent-1",
		CustomerId:        "cust-1",
		CustomerFirstName: "Jane",
		CustomerLastName:  "Doe",
		CustomerIp:        "203.0.113.10",
		AmountMinor:       1500,
		Currency:          "RUB",
		CardPan:           "4111111111111111",
		CardHolder:        "JANE DOE",
		CardExpMonth:      12,
		CardExpYear:       2035,
	}
}

func validCardTokenPayoutRequest() *mntxv1.CardTokenPayoutRequest {
	return &mntxv1.CardTokenPayoutRequest{
		PayoutId:          "payout-1",
		ParentPaymentRef:  "payment-parent-1",
		CustomerId:        "cust-1",
		CustomerFirstName: "Jane",
		CustomerLastName:  "Doe",
		CustomerIp:        "203.0.113.11",
		AmountMinor:       2500,
		Currency:          "USD",
		CardToken:         "tok_123",
	}
}

func validCardTokenizeRequest() *mntxv1.CardTokenizeRequest {
	month, year := futureExpiry()
	return &mntxv1.CardTokenizeRequest{
		RequestId:         "req-1",
		CustomerId:        "cust-1",
		CustomerFirstName: "Jane",
		CustomerLastName:  "Doe",
		CustomerIp:        "203.0.113.12",
		CardPan:           "4111111111111111",
		CardHolder:        "JANE DOE",
		CardCvv:           "123",
		CardExpMonth:      month,
		CardExpYear:       year,
	}
}

func futureExpiry() (uint32, uint32) {
	now := time.Now().UTC()
	return uint32(now.Month()), uint32(now.Year() + 1) //nolint:gosec // month/year values are bounded by time.Time
}
