package com.gxwebsoft.test;

import com.gxwebsoft.common.core.config.CertificateProperties;
import com.gxwebsoft.common.core.utils.CertificateLoader;
import com.gxwebsoft.common.core.utils.WechatCertAutoConfig;
import com.wechat.pay.java.core.Config;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

/**
 * 证书测试类
 */
@SpringBootTest
@ActiveProfiles("dev")
public class CertificateTest {

    @Autowired
    private CertificateProperties certConfig;

    @Autowired
    private CertificateLoader certificateLoader;

    @Autowired
    private WechatCertAutoConfig wechatCertAutoConfig;

    @Test
    public void testCertificateLoading() {
        try {
            System.out.println("=== 证书加载测试 ===");

            // 测试租户ID
            String tenantId = "10550";
            String tenantCertPath = "dev/wechat/" + tenantId;
            String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();

            System.out.println("证书路径: " + privateKeyPath);
            System.out.println("加载模式: " + certConfig.getLoadMode());
            System.out.println("开发环境证书路径前缀: " + certConfig.getDevCertPath());

            // 检查证书文件是否存在
            boolean exists = certificateLoader.certificateExists(privateKeyPath);
            System.out.println("证书文件是否存在: " + exists);

            if (!exists) {
                System.err.println("❌ 证书文件不存在: " + privateKeyPath);
                System.out.println("💡 请确认证书文件已放置在正确位置:");
                System.out.println("   src/main/resources/dev/wechat/10550/apiclient_key.pem");
                return;
            }

            // 测试证书加载
            String privateKeyFile = certificateLoader.loadCertificatePath(privateKeyPath);
            System.out.println("✅ 私钥文件加载成功: " + privateKeyFile);

            // 测试自动证书配置
            System.out.println("=== 测试自动证书配置 ===");
            Config config = wechatCertAutoConfig.createAutoConfig(
                "1723321338", // 测试商户号
                privateKeyFile,
                "test-serial-number", // 测试序列号
                "test-api-key" // 测试API密钥
            );

            System.out.println("✅ 自动证书配置创建成功");

        } catch (Exception e) {
            System.err.println("❌ 证书测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    @Test
    public void testNotificationCertificateConfig() {
        try {
            System.out.println("=== 异步通知证书配置测试 ===");

            // 模拟异步通知中的证书配置逻辑
            String tenantId = "10550";
            String tenantCertPath = "dev/wechat/" + tenantId;
            String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();

            System.out.println("租户ID: " + tenantId);
            System.out.println("证书目录: " + tenantCertPath);
            System.out.println("私钥路径: " + privateKeyPath);

            // 检查证书文件是否存在
            if (!certificateLoader.certificateExists(privateKeyPath)) {
                System.err.println("❌ 证书文件不存在: " + privateKeyPath);
                throw new RuntimeException("证书文件不存在: " + privateKeyPath);
            }

            String privateKey = certificateLoader.loadCertificatePath(privateKeyPath);
            String apiV3Key = certConfig.getWechatPay().getDev().getApiV3Key();

            System.out.println("✅ 私钥文件加载成功: " + privateKey);
            System.out.println("APIv3密钥配置: " + (apiV3Key != null ? "已配置" : "未配置"));

            System.out.println("✅ 异步通知证书配置测试通过");

        } catch (Exception e) {
            System.err.println("❌ 异步通知证书配置测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
