package payment

import (
	"strings"
	"testing"

	"kra/internal/biz"
)

func TestValidatePaymentConfigRequiresDouyinAppIDWhenEnabled(t *testing.T) {
	values := map[string]any{
		"merchant_id": "merchant-douyin", "serial_no": "merchant-serial", "api_key": "01234567890123456789012345678901",
		"private_key": "merchant-private-key", "platform_cert": "platform-public-key", "platform_serial_no": "platform-serial",
	}
	err := biz.ValidateIntegrationConfig(biz.IntegrationKindPayment, biz.PaymentDouyin, values)
	if err == nil || !strings.Contains(err.Error(), "app_id") {
		t.Fatalf("missing app_id error = %v", err)
	}
	values["app_id"] = "douyin-app"
	if err = biz.ValidateIntegrationConfig(biz.IntegrationKindPayment, biz.PaymentDouyin, values); err != nil {
		t.Fatalf("valid Douyin configuration rejected: %v", err)
	}
}

func TestValidatePaymentConfigAcceptsProviderAliases(t *testing.T) {
	err := validatePaymentConfig(biz.PaymentDouyin, map[string]any{
		"app_id": "douyin-app", "merchant_id": "merchant-douyin", "serial_no": "merchant-serial", "api_key": "01234567890123456789012345678901",
		"private_key": "merchant-private-key", "platform_cert": "platform-public-key", "platform_cert_serial": "platform-serial",
	})
	if err != nil {
		t.Fatalf("platform_cert_serial alias rejected: %v", err)
	}
}

func TestValidatePaymentConfigProviderRules(t *testing.T) {
	tests := []struct {
		name, provider, want string
		values               map[string]any
	}{
		{"allinpay order type", biz.PaymentAllinPay, "reqsn", map[string]any{"cus_id": "customer", "app_id": "app", "private_key": "private-key", "public_key": "public-key", "query_order_type": "payinfo"}},
		{"paypal webhook", biz.PaymentPayPal, "webhook_id", map[string]any{"client_id": "client-id", "client_secret": "client-secret"}},
		{"wechat v2 refund cert", biz.PaymentWechatV2, "client_cert", map[string]any{"app_id": "app", "merchant_id": "merchant", "mch_key": "key"}},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			err := biz.ValidateIntegrationConfig(biz.IntegrationKindPayment, test.provider, test.values)
			if err == nil || !strings.Contains(err.Error(), test.want) {
				t.Fatalf("error = %v, want %q", err, test.want)
			}
		})
	}
}

func TestValidatePaymentConfigGenericRequiresRuntimeFields(t *testing.T) {
	values := biz.DefaultIntegrationConfig(biz.IntegrationKindPayment, biz.PaymentChinaums)
	if err := biz.ValidateIntegrationConfig(biz.IntegrationKindPayment, biz.PaymentChinaums, values); err == nil {
		t.Fatal("empty generic payment config unexpectedly accepted")
	}
	values["app_id"], values["merchant_id"] = "app", "merchant"
	values["create_url"], values["query_url"], values["refund_url"], values["app_key"] = "https://pay.test/create", "https://pay.test/query", "https://pay.test/refund", "secret"
	if err := biz.ValidateIntegrationConfig(biz.IntegrationKindPayment, biz.PaymentChinaums, values); err == nil {
		t.Fatal("generic config with only identity/endpoints unexpectedly accepted")
	}
}
