package com.seer.teach.pay.app.config;

import com.seer.teach.common.utils.AESUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信支付配置加密器
 * 用于加密微信支付的敏感配置参数
 */
@Slf4j
@Component
public class PayConfigEncryptor {

    // 需要加密的敏感字段列表
    private static final List<String> SENSITIVE_FIELDS = Arrays.asList(
        "appId",
        "mchId", 
        "mchKey",
        "secret",
        "apiV3Key",
        "publicKeyId",
        "merchantSerialNumber",
        "publicKeyPem",
        "privateKeyPem"
    );

    /**
     * 加密配置参数
     * 
     * @param configMap 配置参数Map
     * @return 加密后的配置参数Map
     */
    public Map<String, Object> encryptConfig(Map<String, Object> configMap) {
        if (configMap == null || configMap.isEmpty()) {
            return configMap;
        }

        Map<String, Object> encryptedConfig = new HashMap<>();
        for (Map.Entry<String, Object> entry : configMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            // 检查是否为敏感字段
            boolean isSensitive = false;
            for (String sensitiveField : SENSITIVE_FIELDS) {
                if (sensitiveField.equals(key)) {
                    isSensitive = true;
                    break;
                }
            }

            if (isSensitive && value instanceof String && value != null && !((String) value).isEmpty()) {
                // 对敏感字段进行加密
                try {
                    String encryptedValue = AESUtils.encrypt((String) value);
                    encryptedConfig.put(key, encryptedValue);
                    log.info("字段 {} 加密成功", key);
                } catch (Exception e) {
                    log.error("字段 {} 加密失败: {}", key, e.getMessage(), e);
                    // 加密失败时保留原始值
                    encryptedConfig.put(key, value);
                }
            } else {
                // 非敏感字段保持不变
                encryptedConfig.put(key, value);
            }
        }

        return encryptedConfig;
    }

    /**
     * 主方法，用于生成加密后的配置参数
     */
    public static void main(String[] args) {
        PayConfigEncryptor encryptor = new PayConfigEncryptor();
        
        // 微信支付配置参数
        Map<String, Object> config = new HashMap<>();
        config.put("appId", "wx04019010242d540e");
        config.put("mchId", "1724072986");
        config.put("mchKey", "9c0c0a5c5d5c5d5c5d5c5d5c5d5c5d5c5d5c5d5");
        config.put("secret", "d9280058237ee5e3dd81bafd72e5cf3b");
        config.put("apiV3Key", "gzzsseerteach20250811zengwei1397");
        config.put("wxLoginUrl", "https://api.weixin.qq.com/sns/jscode2session");
        config.put("publicKeyId", "PUB_KEY_ID_0117240729862025081100212138000608");
        config.put("getQrCodeUrl", "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=");
        config.put("publicKeyPem", "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6vVFD9t19OWseTkOlCklUbr/T7UnEcxQhESQMNW/BASJyrFHKHuZ+zXSyTksj07wu7027mx7QWRc7WDJRb0nVqDrNEbUtPoCzuH2rNTFuSwj+bdIV77f7AdT+3ahzKgzmizG0sWF/ecDfX7b/JGHI3ij4FbTPJM7NyB6OEKFXzkChtScosvQr+1oKiNU3L76QvMJUIrNvSqyUUmF7n83882hc5mCcFYS14HRGUUfl6kA4P4LHv/HfyFw7VPHdh9Rnc5mwBWV5LxFOeKULVZCHO8cX83lYiyK3koqKNnLYju6fKCMmeD8GqQECRAT39TgjLP/i+lI0b0D94sXCj+UBQIDAQAB\n-----END PUBLIC KEY-----");
        config.put("privateKeyPem", "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCwJh5ug8kMnLpdo4Sl4/19JpKId4WsJgrRMLX3W9LjPhiZtiGTUgOY+geUwtFulCDy25c2WpICC5bet3dAUzDQrhbH3uTgQd9rOU97VwBwE+4HSVCdlVcLN18ZbcWAHA28XW5h0KKID0LSwG58agCDd/fR/W0KOwzh+lh1zhowGfftcsWNFPQJ55KXn9ZGSJ8B82vgg8w2bDKw/nqEkStMSTU2+yKeNQonguRVhpQC3v+CPDBXHbnFTgCt9tFZ3eNXhOwC3VH2JTwfo1UP+6tb50pSU0dSbLukeff2HkEO+NrXgd723PXHbQXe7IV8fREo1yxEd29P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Iw0qx08AwHg+YdwPSOYSGTmStQVB/eNbFpsHH/6+sYoVBnzydUcDLzLs4/EGKbVoiklrOlgXpmNJcw55mHo4J9PVPmM2jpSIimeNyJImt6qUCPQdNoBrsxuzcSfcMSBYf2XOyLlu7FnlA6ceGthkCqXQ3qyweJFacjuRLwn\n-----END PRIVATE KEY-----");
        config.put("jsPayNotifyUrl", "https://zs.seerteach.net/seer/pay/app/pay/order/callback/wechat");
        config.put("redisAccessKey", "wxAccessToken");
        config.put("getAccessTokenUrl", "https://api.weixin.qq.com/cgi-bin/token");
        config.put("jsRefundNotifyUrl", "https://zs.seerteach.cn/wechat/wallet/refund/notify");
        config.put("merchantSerialNumber", "4CB3BF0F84BEA6E6ACCEBC41ABEBFA73455D5162");

        // 加密配置参数
        Map<String, Object> encryptedConfig = encryptor.encryptConfig(config);

        // 输出加密后的配置参数
        System.out.println("加密后的配置参数:");
        System.out.println("{");
        for (Map.Entry<String, Object> entry : encryptedConfig.entrySet()) {
            System.out.println("  \"" + entry.getKey() + "\": \"" + entry.getValue() + "\",");
        }
        System.out.println("}");
        
        // 单独输出每个敏感字段的加密值，方便您复制
        System.out.println("\n各个敏感字段的加密值:");
        for (String sensitiveField : SENSITIVE_FIELDS) {
            Object value = encryptedConfig.get(sensitiveField);
            if (value != null) {
                System.out.println(sensitiveField + ": " + value);
            }
        }
    }
}