package gateway

import "testing"

func TestPayoutFailurePolicy_DecideProviderFailure(t *testing.T) {
	policy := defaultPayoutFailurePolicy()

	cases := []struct {
		name   string
		code   string
		action payoutFailureAction
	}{
		{
			name:   "retryable provider limit code",
			code:   providerCodeDeclineAmountOrFrequencyLimit,
			action: payoutFailureActionRetry,
		},
		{
			name:   "unknown provider code",
			code:   "99999",
			action: payoutFailureActionFail,
		},
		{
			name:   "empty provider code",
			code:   "",
			action: payoutFailureActionFail,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			t.Helper()
			got := policy.decideProviderFailure(tc.code)
			if got.Action != tc.action {
				t.Fatalf("action mismatch: got=%v want=%v", got.Action, tc.action)
			}
		})
	}
}

func TestPayoutFailureReason(t *testing.T) {
	if got, want := payoutFailureReason("10101", "Decline due to amount or frequency limit"), "10101: Decline due to amount or frequency limit"; got != want {
		t.Fatalf("failure reason mismatch: got=%q want=%q", got, want)
	}
	if got, want := payoutFailureReason("", "network error"), "network error"; got != want {
		t.Fatalf("failure reason mismatch: got=%q want=%q", got, want)
	}
	if got, want := payoutFailureReason("10101", ""), "10101"; got != want {
		t.Fatalf("failure reason mismatch: got=%q want=%q", got, want)
	}
}
