package com.gxwebsoft.test;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 证书路径修复验证测试
 * 
 * @author 科技小王子
 * @since 2025-08-09
 */
@SpringBootTest
@ActiveProfiles("dev")
public class CertificatePathFixTest {

    private static final String CERT_BASE_PATH = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550";
    
    @Test
    public void testCertificatePathFix() {
        System.out.println("=== 证书路径修复验证测试 ===");
        
        // 验证证书目录存在
        File certDir = new File(CERT_BASE_PATH);
        assert certDir.exists() && certDir.isDirectory() : "证书目录不存在: " + CERT_BASE_PATH;
        System.out.println("✅ 证书目录存在: " + CERT_BASE_PATH);
        
        // 验证私钥文件
        String privateKeyPath = CERT_BASE_PATH + "/apiclient_key.pem";
        File privateKeyFile = new File(privateKeyPath);
        assert privateKeyFile.exists() && privateKeyFile.isFile() : "私钥文件不存在: " + privateKeyPath;
        System.out.println("✅ 私钥文件存在: " + privateKeyPath);
        System.out.println("   文件大小: " + privateKeyFile.length() + " bytes");
        
        // 验证证书文件
        String certPath = CERT_BASE_PATH + "/apiclient_cert.pem";
        File certFile = new File(certPath);
        assert certFile.exists() && certFile.isFile() : "证书文件不存在: " + certPath;
        System.out.println("✅ 证书文件存在: " + certPath);
        System.out.println("   文件大小: " + certFile.length() + " bytes");
        
        // 验证文件内容格式
        try {
            String privateKeyContent = Files.readString(Paths.get(privateKeyPath));
            assert privateKeyContent.contains("-----BEGIN PRIVATE KEY-----") : "私钥文件格式错误";
            System.out.println("✅ 私钥文件格式正确");
            
            String certContent = Files.readString(Paths.get(certPath));
            assert certContent.contains("-----BEGIN CERTIFICATE-----") : "证书文件格式错误";
            System.out.println("✅ 证书文件格式正确");
            
        } catch (Exception e) {
            throw new RuntimeException("读取证书文件失败: " + e.getMessage(), e);
        }
        
        System.out.println("=== 证书路径修复验证完成 ===");
        System.out.println("🎉 所有证书文件验证通过！");
        System.out.println();
        System.out.println("📋 修复内容总结:");
        System.out.println("1. 修复了 SettingServiceImpl 中的硬编码证书路径");
        System.out.println("2. 更新了 WechatCertAutoConfig 中的默认开发环境配置");
        System.out.println("3. 证书路径已指向正确位置: " + CERT_BASE_PATH);
        System.out.println();
        System.out.println("🔧 下一步建议:");
        System.out.println("1. 重启应用程序以使配置生效");
        System.out.println("2. 测试微信支付功能是否正常工作");
        System.out.println("3. 检查应用日志确认证书加载成功");
    }
    
    @Test
    public void testCertificatePathStructure() {
        System.out.println("=== 证书目录结构验证 ===");
        
        File baseDir = new File("/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat");
        if (baseDir.exists()) {
            File[] tenantDirs = baseDir.listFiles(File::isDirectory);
            if (tenantDirs != null) {
                System.out.println("发现租户证书目录:");
                for (File tenantDir : tenantDirs) {
                    System.out.println("  - 租户ID: " + tenantDir.getName());
                    
                    File privateKey = new File(tenantDir, "apiclient_key.pem");
                    File cert = new File(tenantDir, "apiclient_cert.pem");
                    File p12 = new File(tenantDir, "apiclient_cert.p12");
                    
                    System.out.println("    私钥文件: " + (privateKey.exists() ? "✅" : "❌"));
                    System.out.println("    证书文件: " + (cert.exists() ? "✅" : "❌"));
                    System.out.println("    P12文件: " + (p12.exists() ? "✅" : "❌"));
                }
            }
        }
        
        System.out.println("=== 目录结构验证完成 ===");
    }
}
