package com.gxwebsoft.common.core;

import com.gxwebsoft.common.core.service.CertificateService;
import com.gxwebsoft.common.core.service.CertificateHealthService;
import com.gxwebsoft.common.core.config.CertificateProperties;
import lombok.extern.slf4j.Slf4j;
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;

import java.util.Map;

/**
 * 支付配置测试类
 * 用于测试支付配置缓存注释和证书路径打印功能
 */
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class PaymentConfigTest {

    @Autowired
    private CertificateService certificateService;

    @Autowired
    private CertificateProperties certificateProperties;

    @Autowired
    private CertificateHealthService certificateHealthService;

    @Test
    public void testWechatPayCertPath() {
        log.info("=== 测试微信支付证书路径获取 ===");

        try {
            String privateKeyFile = certificateProperties.getWechatPay().getDev().getPrivateKeyFile();
            log.info("配置的私钥文件名: {}", privateKeyFile);

            String certPath = certificateService.getWechatPayCertPath(privateKeyFile);
            log.info("获取到的证书路径: {}", certPath);

            // 测试证书是否存在
            boolean exists = certificateService.certificateExists("wechat", privateKeyFile);
            log.info("证书文件是否存在: {}", exists);

        } catch (Exception e) {
            log.error("测试微信支付证书路径失败: {}", e.getMessage(), e);
        }
    }

    @Test
    public void testAlipayCertPath() {
        log.info("=== 测试支付宝证书路径获取 ===");

        try {
            CertificateProperties.AlipayConfig alipayConfig = certificateProperties.getAlipay();

            String appCertFile = alipayConfig.getAppCertPublicKeyFile();
            String alipayCertFile = alipayConfig.getAlipayCertPublicKeyFile();
            String rootCertFile = alipayConfig.getAlipayRootCertFile();

            log.info("配置的应用证书文件名: {}", appCertFile);
            log.info("配置的支付宝证书文件名: {}", alipayCertFile);
            log.info("配置的根证书文件名: {}", rootCertFile);

            String appCertPath = certificateService.getAlipayCertPath(appCertFile);
            String alipayCertPath = certificateService.getAlipayCertPath(alipayCertFile);
            String rootCertPath = certificateService.getAlipayCertPath(rootCertFile);

            log.info("获取到的应用证书路径: {}", appCertPath);
            log.info("获取到的支付宝证书路径: {}", alipayCertPath);
            log.info("获取到的根证书路径: {}", rootCertPath);

        } catch (Exception e) {
            log.error("测试支付宝证书路径失败: {}", e.getMessage(), e);
        }
    }

    @Test
    public void testCertificateProperties() {
        log.info("=== 测试证书配置属性 ===");

        log.info("证书加载模式: {}", certificateProperties.getLoadMode());
        log.info("证书根路径: {}", certificateProperties.getCertRootPath());
        log.info("开发环境证书路径: {}", certificateProperties.getDevCertPath());
        log.info("是否为classpath模式: {}", certificateProperties.isClasspathMode());

        // 微信支付配置
        CertificateProperties.WechatPayConfig wechatPay = certificateProperties.getWechatPay();
        log.info("微信支付证书目录: {}", wechatPay.getCertDir());
        log.info("微信支付APIv3密钥: {}", wechatPay.getDev().getApiV3Key() != null ? "已配置" : "未配置");
        log.info("微信支付私钥文件: {}", wechatPay.getDev().getPrivateKeyFile());

        // 支付宝配置
        CertificateProperties.AlipayConfig alipay = certificateProperties.getAlipay();
        log.info("支付宝证书目录: {}", alipay.getCertDir());
        log.info("支付宝应用证书文件: {}", alipay.getAppCertPublicKeyFile());
    }

    @Test
    public void testDatabaseCertificateCheck() {
        log.info("=== 测试数据库证书配置检查 ===");

        try {
            Map<String, Object> result = certificateHealthService.checkDatabaseCertificates();
            log.info("数据库证书检查结果: {}", result);

            Boolean healthy = (Boolean) result.get("healthy");
            log.info("证书健康状态: {}", healthy ? "健康" : "不健康");

            @SuppressWarnings("unchecked")
            Map<String, Object> certificates = (Map<String, Object>) result.get("certificates");
            if (certificates != null) {
                log.info("证书详细信息:");
                certificates.forEach((key, value) -> {
                    log.info("  {}: {}", key, value);
                });
            }

        } catch (Exception e) {
            log.error("测试数据库证书检查失败: {}", e.getMessage(), e);
        }
    }

    @Test
    public void testCertificateHealthCheck() {
        log.info("=== 测试完整证书健康检查 ===");

        try {
            CertificateHealthService.HealthResult healthResult = certificateHealthService.health();
            log.info("证书健康检查状态: {}", healthResult.getStatus());
            log.info("证书健康检查详情: {}", healthResult.getDetails());

        } catch (Exception e) {
            log.error("测试证书健康检查失败: {}", e.getMessage(), e);
        }
    }
}
