package com.gxwebsoft.test;

import com.gxwebsoft.common.core.config.CertificateProperties;
import com.gxwebsoft.common.core.utils.CertificateLoader;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.system.entity.Payment;
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 WechatPayConfigValidationTest {

    @Autowired
    private CertificateProperties certConfig;

    @Autowired
    private CertificateLoader certificateLoader;

    @Autowired
    private RedisUtil redisUtil;

    @Test
    public void testWechatPayConfigValidation() {
        System.out.println("=== 微信支付配置验证 ===");
        
        // 1. 检查配置文件中的 APIv3 密钥
        String configApiV3Key = certConfig.getWechatPay().getDev().getApiV3Key();
        System.out.println("配置文件 APIv3 密钥: " + configApiV3Key);
        System.out.println("配置文件 APIv3 密钥长度: " + (configApiV3Key != null ? configApiV3Key.length() : 0));
        
        // 2. 检查 Redis 中的支付配置
        String tenantId = "10550";
        String redisKey = "Payment:1:" + tenantId;
        Payment payment = redisUtil.get(redisKey, Payment.class);
        
        if (payment != null) {
            System.out.println("\n=== Redis 支付配置 ===");
            System.out.println("商户号: " + payment.getMchId());
            System.out.println("应用ID: " + payment.getAppId());
            System.out.println("数据库 APIv3 密钥: " + payment.getApiKey());
            System.out.println("数据库 APIv3 密钥长度: " + (payment.getApiKey() != null ? payment.getApiKey().length() : 0));
            System.out.println("商户证书序列号: " + payment.getMerchantSerialNumber());
            
            // 3. 比较两个 APIv3 密钥
            System.out.println("\n=== APIv3 密钥比较 ===");
            boolean keysMatch = (configApiV3Key != null && configApiV3Key.equals(payment.getApiKey()));
            System.out.println("配置文件与数据库密钥是否一致: " + keysMatch);
            
            if (!keysMatch) {
                System.out.println("⚠️ 警告: 配置文件与数据库中的 APIv3 密钥不一致!");
                System.out.println("配置文件密钥: " + configApiV3Key);
                System.out.println("数据库密钥: " + payment.getApiKey());
            }
            
        } else {
            System.out.println("❌ 未找到 Redis 中的支付配置: " + redisKey);
            
            // 尝试其他可能的键格式
            String[] possibleKeys = {
                "Payment:1:10550",
                "Payment:0:10550", 
                "Payment:10",
                "Payment:1" + "0"  // Payment:10
            };
            
            System.out.println("\n=== 尝试其他 Redis 键格式 ===");
            for (String key : possibleKeys) {
                Payment p = redisUtil.get(key, Payment.class);
                if (p != null) {
                    System.out.println("✅ 找到支付配置: " + key);
                    System.out.println("   商户号: " + p.getMchId());
                    System.out.println("   APIv3密钥: " + p.getApiKey());
                    break;
                }
            }
        }
        
        // 4. 验证证书文件
        System.out.println("\n=== 证书文件验证 ===");
        String tenantCertPath = "dev/wechat/" + tenantId;
        String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
        
        boolean certExists = certificateLoader.certificateExists(privateKeyPath);
        System.out.println("证书文件存在: " + certExists);
        System.out.println("证书路径: " + privateKeyPath);
        
        if (certExists) {
            try {
                String privateKey = certificateLoader.loadCertificatePath(privateKeyPath);
                System.out.println("✅ 证书加载成功: " + privateKey);
            } catch (Exception e) {
                System.out.println("❌ 证书加载失败: " + e.getMessage());
            }
        }
        
        System.out.println("\n=== 验证完成 ===");
    }
    
    @Test
    public void testApiV3KeyValidation() {
        System.out.println("=== APIv3 密钥格式验证 ===");
        
        String configKey = certConfig.getWechatPay().getDev().getApiV3Key();
        
        if (configKey != null) {
            System.out.println("APIv3 密钥: " + configKey);
            System.out.println("密钥长度: " + configKey.length());
            
            // APIv3 密钥应该是32位字符串
            if (configKey.length() == 32) {
                System.out.println("✅ APIv3 密钥长度正确 (32位)");
            } else {
                System.out.println("❌ APIv3 密钥长度错误，应为32位，实际为: " + configKey.length());
            }
            
            // 检查是否包含特殊字符
            boolean hasSpecialChars = !configKey.matches("^[a-zA-Z0-9]+$");
            if (hasSpecialChars) {
                System.out.println("⚠️ APIv3 密钥包含特殊字符，可能导致解密失败");
            } else {
                System.out.println("✅ APIv3 密钥格式正确 (仅包含字母和数字)");
            }
            
        } else {
            System.out.println("❌ APIv3 密钥未配置");
        }
    }
}
