package payment

import (
	bizpayment "kra/internal/biz/payment"
	"testing"
)

func TestEverySupportedProviderHasAdapter(t *testing.T) {
	for _, provider := range bizpayment.SupportedPaymentProviders {
		t.Run(provider, func(t *testing.T) {
			adapter, err := New(provider)
			if err != nil {
				t.Fatalf("New(%q): %v", provider, err)
			}
			if adapter == nil {
				t.Fatalf("New(%q) returned a nil adapter", provider)
			}
		})
	}
}

func TestNewNormalizesProviderIdentifier(t *testing.T) {
	for _, provider := range bizpayment.SupportedPaymentProviders {
		adapter, err := New("  " + provider + "  ")
		if err != nil {
			t.Fatalf("New(%q): %v", provider, err)
		}
		if adapter == nil {
			t.Fatalf("New(%q) returned nil", provider)
		}
	}
}

func TestNewRejectsUnknownProvider(t *testing.T) {
	if _, err := New("unknown-provider"); err == nil {
		t.Fatal("unknown provider unexpectedly accepted")
	}
}

func TestNonceIsUniqueForConcurrentRequests(t *testing.T) {
	const count = 1000
	values := make(chan string, count)
	for i := 0; i < count; i++ {
		go func() { values <- nonce() }()
	}
	seen := make(map[string]struct{}, count)
	for i := 0; i < count; i++ {
		value := <-values
		if value == "" {
			t.Fatal("nonce returned an empty value")
		}
		if _, exists := seen[value]; exists {
			t.Fatalf("nonce collision for %q", value)
		}
		seen[value] = struct{}{}
	}
}
