package payments

import "testing"

func TestPurchaseGate(t *testing.T) {
	on := RailAvailability{Enabled: true}
	offMsg := RailAvailability{Enabled: false, MessageRU: "Чиним", MessageEN: "Fixing"}
	offNoMsg := RailAvailability{Enabled: false}

	// OverrideDefault follows the rail switch.
	if ok, _ := PurchaseGate(OverrideDefault, on, "en"); !ok {
		t.Error("default + enabled rail should allow")
	}
	if ok, r := PurchaseGate(OverrideDefault, offMsg, "ru"); ok || r != "Чиним" {
		t.Errorf("default + off rail (ru) = %v/%q, want false/Чиним", ok, r)
	}
	if ok, r := PurchaseGate(OverrideDefault, offMsg, "en"); ok || r != "Fixing" {
		t.Errorf("default + off rail (en) = %v/%q, want false/Fixing", ok, r)
	}

	// Off rail with no custom message → the built-in default, localized (not the English default in ru).
	if ok, r := PurchaseGate(OverrideDefault, offNoMsg, "ru"); ok || r != defaultUnavailable("ru") {
		t.Errorf("default + off no-msg (ru) = %v/%q, want false + the ru default", ok, r)
	}

	// OverrideAllow bypasses the ops switch even when the rail is off.
	if ok, r := PurchaseGate(OverrideAllow, offMsg, "en"); !ok || r != "" {
		t.Errorf("allow + off rail = %v/%q, want true/empty (bypasses the ops switch)", ok, r)
	}

	// OverrideDeny blocks even when the rail is on, with a non-empty reason.
	if ok, r := PurchaseGate(OverrideDeny, on, "en"); ok || r == "" {
		t.Errorf("deny + on rail = %v/%q, want false + a reason", ok, r)
	}

	// The message falls back to the other language when only one is set.
	if _, r := PurchaseGate(OverrideDefault, RailAvailability{MessageEN: "OnlyEN"}, "ru"); r != "OnlyEN" {
		t.Errorf("off rail ru with only EN msg = %q, want OnlyEN (fallback)", r)
	}
}
