package config

import (
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"strings"
	"testing"
)

func TestLoadLocalEnablesOutboxMQ(t *testing.T) {
	cfg, err := Load("../../configs/config.yaml")
	if err != nil {
		t.Fatalf("Load local config failed: %v", err)
	}
	// 本地默认发布普通钱包事实和实时红包事实，链路和 Docker/testbox/线上保持一致。
	if !cfg.RocketMQ.Enabled || !cfg.RocketMQ.WalletOutbox.Enabled || !cfg.RocketMQ.RealtimeOutbox.Enabled {
		t.Fatalf("local config must enable wallet outbox MQ producers: %+v", cfg.RocketMQ)
	}
	if !cfg.OutboxWorker.Enabled || !cfg.RealtimeOutboxWorker.Enabled || !cfg.ProjectionWorker.Enabled {
		t.Fatalf("local config must enable wallet outbox workers: outbox=%+v realtime=%+v projection=%+v", cfg.OutboxWorker, cfg.RealtimeOutboxWorker, cfg.ProjectionWorker)
	}
	if cfg.OutboxWorker.Concurrency != 1 || cfg.RealtimeOutboxWorker.Concurrency != 2 {
		t.Fatalf("local outbox worker defaults mismatch: outbox=%+v realtime=%+v", cfg.OutboxWorker, cfg.RealtimeOutboxWorker)
	}
}

func TestLoadTencentExampleSplitsRealtimeOutboxAndRaisesThroughput(t *testing.T) {
	cfg, err := Load("../../configs/config.tencent.example.yaml")
	if err != nil {
		t.Fatalf("Load tencent example failed: %v", err)
	}
	if !cfg.RocketMQ.Enabled || !cfg.RocketMQ.WalletOutbox.Enabled || !cfg.RocketMQ.RealtimeOutbox.Enabled {
		t.Fatalf("tencent example must enable both wallet outbox producers: %+v", cfg.RocketMQ)
	}
	if cfg.RocketMQ.WalletOutbox.Topic == cfg.RocketMQ.RealtimeOutbox.Topic {
		t.Fatalf("realtime outbox topic must be separated from generic wallet topic: %+v", cfg.RocketMQ)
	}
	if cfg.OutboxWorker.BatchSize != 200 || cfg.OutboxWorker.Concurrency != 8 {
		t.Fatalf("generic outbox throughput config mismatch: %+v", cfg.OutboxWorker)
	}
	if cfg.RealtimeOutboxWorker.BatchSize != 50 || cfg.RealtimeOutboxWorker.Concurrency != 4 {
		t.Fatalf("realtime outbox worker config mismatch: %+v", cfg.RealtimeOutboxWorker)
	}
	if len(cfg.RealtimeOutboxWorker.EventTypes) != 3 || cfg.RealtimeOutboxWorker.EventTypes[0] != "WalletRedPacketCreated" {
		t.Fatalf("realtime outbox event types mismatch: %+v", cfg.RealtimeOutboxWorker.EventTypes)
	}
}

func TestLoadMifaPayMerchantConfig(t *testing.T) {
	for _, path := range []string{
		"../../configs/config.yaml",
		"../../configs/config.docker.yaml",
		"../../configs/config.tencent.example.yaml",
	} {
		cfg, err := Load(path)
		if err != nil {
			t.Fatalf("Load %s failed: %v", path, err)
		}
		if cfg.MifaPay.MerNo != "10001910" || cfg.MifaPay.MerAccount != "6000055d34f445418124ab7c0beb81a6" {
			t.Fatalf("mifapay merchant identity mismatch in %s: %+v", path, cfg.MifaPay)
		}
		assertRSAPublicKey(t, path+" merchant_public_key", cfg.MifaPay.MerchantPublicKey)
		assertRSAPublicKey(t, path+" platform_public_key", cfg.MifaPay.PlatformPublicKey)
	}
}

func assertRSAPublicKey(t *testing.T, name string, value string) {
	t.Helper()
	clean := strings.NewReplacer("\n", "", "\r", "", " ", "").Replace(strings.TrimSpace(value))
	raw, err := base64.StdEncoding.DecodeString(clean)
	if err != nil {
		t.Fatalf("%s should be base64 DER public key: %v", name, err)
	}
	key, err := x509.ParsePKIXPublicKey(raw)
	if err != nil {
		t.Fatalf("%s should parse as PKIX public key: %v", name, err)
	}
	if _, ok := key.(*rsa.PublicKey); !ok {
		t.Fatalf("%s should be RSA public key, got %T", name, key)
	}
}
