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.gxwebsoft.common.core.service.PaymentCacheService;
import com.gxwebsoft.common.system.entity.Payment;
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 WechatPayConfigTest {

    @Autowired
    private CertificateProperties certConfig;

    @Autowired
    private CertificateLoader certificateLoader;

    @Autowired
    private WechatCertAutoConfig wechatCertAutoConfig;

    @Autowired
    private PaymentCacheService paymentCacheService;

    @Test
    public void testWechatPayConfig() {
        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());

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

            // 测试数据库支付配置
            System.out.println("=== 测试数据库支付配置 ===");
            try {
                Payment payment = paymentCacheService.getPaymentConfig(0, 10550); // 微信支付，租户ID 10550
                System.out.println("数据库配置获取成功:");
                System.out.println("商户号: " + payment.getMchId());
                System.out.println("序列号: " + payment.getMerchantSerialNumber());
                System.out.println("API密钥: " + (payment.getApiKey() != null ? "已配置(长度:" + payment.getApiKey().length() + ")" : "未配置"));
                System.out.println("应用ID: " + payment.getAppId());

                // 使用数据库配置进行测试
                if (payment.getMchId() != null && payment.getMerchantSerialNumber() != null && payment.getApiKey() != null) {
                    Config dbConfig = wechatCertAutoConfig.createAutoConfig(
                        payment.getMchId(),
                        privateKeyFile,
                        payment.getMerchantSerialNumber(),
                        payment.getApiKey()
                    );
                    System.out.println("使用数据库配置创建成功: " + (dbConfig != null));
                } else {
                    System.out.println("数据库配置不完整，无法创建微信支付配置");
                }

            } catch (Exception e) {
                System.err.println("数据库配置获取失败: " + e.getMessage());

                // 回退到配置文件参数
                System.out.println("=== 回退到配置文件参数 ===");
                String devApiKey = certConfig.getWechatPay().getDev().getApiV3Key();
                System.out.println("API密钥: " + (devApiKey != null ? "已配置(长度:" + devApiKey.length() + ")" : "未配置"));
            }

            System.out.println("=== 测试完成 ===");

        } catch (Exception e) {
            System.err.println("微信支付配置测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
