package cn.hztech.sdk.integration;

import cn.hztech.sdk.client.SdkHttpClient;
import cn.hztech.sdk.crypto.envelope.GmPacketBuilder;
import cn.hztech.sdk.crypto.envelope.GmPacketParser;
import cn.hztech.sdk.crypto.sm2.SM2Util;
import cn.hztech.sdk.crypto.sm4.SM4Util;
import org.junit.jupiter.api.BeforeAll;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import java.util.Base64;

/**
 * 服务端集成测试基类
 * <p>
 * 提供测试所需的公共基础设施：
 * <ul>
 *   <li>HTTP客户端配置</li>
 *   <li>加解密工具初始化</li>
 *   <li>密钥对生成</li>
 * </ul>
 * <p>
 * 前置条件：
 * <ul>
 *   <li>platform-server已启动（端口8080）</li>
 *   <li>使用test profile启动</li>
 * </ul>
 *
 * @author SDK Team
 * @since 1.0.0
 */
public abstract class BaseIntegrationTest {

    /**
     * 测试用HTTP客户端
     */
    protected static SdkHttpClient httpClient;

    /**
     * SM2加解密工具
     */
    protected static SM2Util sm2Util;

    /**
     * SM4加解密工具
     */
    protected static SM4Util sm4Util;

    /**
     * 发送方密钥对
     */
    protected static KeyPair senderKeyPair;

    /**
     * 接收方密钥对
     */
    protected static KeyPair receiverKeyPair;

    /**
     * 网关地址
     */
    protected static final String GATEWAY_URL = "http://localhost:8081";

    /**
     * 应用ID
     */
    protected static final String APP_ID = "TEST_SDK";

    /**
     * 初始化测试环境
     */
    @BeforeAll
    static void setUpBase() throws Exception {
        // 添加BouncyCastle提供者
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        // 初始化加解密工具
        sm2Util = new SM2Util();
        sm4Util = new SM4Util();

        // 生成密钥对
//        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "BC");
//        keyGen.initialize(256);
//        senderKeyPair = keyGen.generateKeyPair();
//        receiverKeyPair = keyGen.generateKeyPair();


        // 获取密钥的Base64编码
//        String senderPrivateKey = Base64.getEncoder().encodeToString(senderKeyPair.getPrivate().getEncoded());
        String senderPrivateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgXmfOgQqOM8g1l80mxioSOQQW4EnxszaSlXRAKd+SQ++gCgYIKoZIzj0DAQehRANCAATYw2RaAi1MJAI18xUzKUvdQV2fqUotQeCIK4D7170Mrq4pei/rGQ1sJAgSX2vq1x3lOT78zZx1Iaus+pHPgMcb";
//        String receiverPublicKey = Base64.getEncoder().encodeToString(receiverKeyPair.getPublic().getEncoded());
        String receiverPublicKey = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEsd97pgeo/HafdkIHkX4pDClD6wwdzohi6WesguFouZHUx+EMx+9kyEHnWlBV2Vq+JpqfrJmJUrMMxip73Ne/WA==";

        // 创建报文构建器和解析器
        GmPacketBuilder packetBuilder = new GmPacketBuilder(APP_ID, senderPrivateKey, receiverPublicKey, sm2Util, sm4Util);
        GmPacketParser packetParser = new GmPacketParser(senderPrivateKey, receiverPublicKey, sm2Util, sm4Util, 60000L);

        // 创建HTTP客户端
        httpClient = new SdkHttpClient(
            GATEWAY_URL,
            10000,  // 连接超时
            30000,  // 读取超时
            30000,  // 写入超时
            packetBuilder,
            packetParser,
            true,   // 启用日志
            false,  // 禁用重试
            0,
            0
        );

        System.out.println("=================================================");
        System.out.println("测试环境初始化完成");
        System.out.println("网关地址: " + GATEWAY_URL);
        System.out.println("应用ID: " + APP_ID);
        System.out.println("=================================================");
    }
}