package gateway

import (
	"testing"

	"github.com/tech/sendico/gateway/aurora/internal/service/provider"
	"github.com/tech/sendico/gateway/aurora/storage/model"
)

func TestPayoutSimulatorResolveByPAN_KnownCard(t *testing.T) {
	sim := newPayoutSimulator()

	scenario := sim.resolveByPAN(scenarioPAN("insufficient_funds", 0))
	if scenario.Name != "insufficient_funds" {
		t.Fatalf("unexpected scenario: got=%q", scenario.Name)
	}
	if scenario.ErrorCode != "51" {
		t.Fatalf("unexpected error code: got=%q", scenario.ErrorCode)
	}
}

func TestPayoutSimulatorResolveByPAN_Default(t *testing.T) {
	sim := newPayoutSimulator()

	scenario := sim.resolveByPAN(defaultScenarioPAN())
	if scenario.Name != "default_processing" {
		t.Fatalf("unexpected default scenario: got=%q", scenario.Name)
	}
	if !scenario.Accepted {
		t.Fatalf("default scenario should be accepted")
	}
}

func TestPayoutSimulatorScenarioPANs_AreLuhnValidAndExpanded(t *testing.T) {
	sim := newPayoutSimulator()

	for _, scenario := range sim.scenarios {
		if got, want := len(scenario.CardNumbers), scenarioPANVariants; got != want {
			t.Fatalf("expected %d PANs for scenario %q, got=%d", want, scenario.Name, got)
		}
		for _, pan := range scenario.CardNumbers {
			if !isValidPANLuhn(pan) {
				t.Fatalf("scenario %q has invalid Luhn PAN %q", scenario.Name, pan)
			}
		}
	}
}

func TestApplyCardPayoutSendResult_AcceptedSuccessStatus(t *testing.T) {
	state := &model.CardPayout{}
	result := &provider.CardPayoutSendResult{
		Accepted:       true,
		ProviderStatus: "success",
		ErrorCode:      "00",
		ErrorMessage:   "Approved",
	}

	applyCardPayoutSendResult(state, result)

	if state.Status != model.PayoutStatusSuccess {
		t.Fatalf("unexpected status: got=%q", state.Status)
	}
	if state.ProviderCode != "00" {
		t.Fatalf("unexpected provider code: got=%q", state.ProviderCode)
	}
}
