package com.gxwebsoft.test;

import com.gxwebsoft.common.core.config.ConfigProperties;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.io.File;

/**
 * 证书路径拼接测试
 * 验证开发环境的路径拼接规则：配置文件upload-path + dev/wechat/ + 租户ID
 *
 * @author 科技小王子
 * @since 2025-08-09
 */
@SpringBootTest
@ActiveProfiles("dev")
public class CertificatePathConcatenationTest {

    @Value("${spring.profiles.active:prod}")
    private String activeProfile;

    @Resource
    private ConfigProperties configProperties;

    @Test
    public void testCertificatePathConcatenation() {
        System.out.println("=== 证书路径拼接测试 ===");
        System.out.println("当前环境: " + activeProfile);

        if ("dev".equals(activeProfile)) {
            testDevEnvironmentPathConcatenation();
        } else {
            testProdEnvironmentPathConcatenation();
        }

        System.out.println("=== 证书路径拼接测试完成 ===");
    }

    private void testDevEnvironmentPathConcatenation() {
        System.out.println("--- 开发环境路径拼接测试 ---");

        // 获取配置文件中的upload-path
        String uploadPath = configProperties.getUploadPath();
        System.out.println("配置文件upload-path: " + uploadPath);

        // 拼接规则：配置文件upload-path + dev/wechat/ + 租户ID
        String tenantId = "10550";
        String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/";
        String privateKeyPath = certBasePath + "apiclient_key.pem";
        String certPath = certBasePath + "apiclient_cert.pem";

        System.out.println("拼接规则: upload-path + dev/wechat/ + 租户ID");
        System.out.println("租户ID: " + tenantId);
        System.out.println("证书基础路径: " + certBasePath);
        System.out.println("私钥文件路径: " + privateKeyPath);
        System.out.println("证书文件路径: " + certPath);

        // 验证路径是否正确
        File privateKeyFile = new File(privateKeyPath);
        File certFile = new File(certPath);

        System.out.println("--- 文件存在性验证 ---");
        System.out.println("私钥文件存在: " + privateKeyFile.exists());
        System.out.println("证书文件存在: " + certFile.exists());

        if (privateKeyFile.exists()) {
            System.out.println("✅ 私钥文件路径拼接正确");
            System.out.println("   文件大小: " + privateKeyFile.length() + " bytes");
        } else {
            System.out.println("❌ 私钥文件路径拼接错误或文件不存在");
        }

        if (certFile.exists()) {
            System.out.println("✅ 证书文件路径拼接正确");
            System.out.println("   文件大小: " + certFile.length() + " bytes");
        } else {
            System.out.println("❌ 证书文件路径拼接错误或文件不存在");
        }

        // 验证期望的路径
        String expectedPath = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550/";
        System.out.println("--- 路径验证 ---");
        System.out.println("期望的证书路径: " + expectedPath);
        System.out.println("实际拼接路径: " + certBasePath);
        System.out.println("路径匹配: " + expectedPath.equals(certBasePath));

        if (expectedPath.equals(certBasePath)) {
            System.out.println("✅ 路径拼接规则正确");
        } else {
            System.out.println("❌ 路径拼接规则需要调整");
            System.out.println("   请检查配置文件中的upload-path设置");
        }
    }

    private void testProdEnvironmentPathConcatenation() {
        System.out.println("--- 生产环境路径配置测试 ---");
        System.out.println("生产环境使用数据库配置的证书路径");
        System.out.println("路径格式: {uploadPath}/file/{relativePath}");

        String uploadPath = configProperties.getUploadPath();
        System.out.println("配置的upload-path: " + uploadPath);

        // 模拟生产环境路径拼接
        String relativePath = "wechat/10550/apiclient_key.pem";
        String prodPath = uploadPath + "file/" + relativePath;
        System.out.println("生产环境示例路径: " + prodPath);
        System.out.println("✅ 生产环境路径配置逻辑正确");
    }

    @Test
    public void testMultipleTenantPaths() {
        System.out.println("=== 多租户路径拼接测试 ===");

        if (!"dev".equals(activeProfile)) {
            System.out.println("跳过：仅在开发环境测试多租户路径");
            return;
        }

        String uploadPath = configProperties.getUploadPath();
        String[] tenantIds = {"10324", "10398", "10547", "10549", "10550"};

        System.out.println("配置文件upload-path: " + uploadPath);
        System.out.println("测试多个租户的证书路径拼接:");

        for (String tenantId : tenantIds) {
            String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/";
            String privateKeyPath = certBasePath + "apiclient_key.pem";

            File privateKeyFile = new File(privateKeyPath);
            System.out.println("租户 " + tenantId + ": " + (privateKeyFile.exists() ? "✅" : "❌") + " " + privateKeyPath);
        }

        System.out.println("=== 多租户路径拼接测试完成 ===");
    }

    @Test
    public void testConfigurationProperties() {
        System.out.println("=== 配置属性测试1 ===");

        System.out.println("当前环境: " + activeProfile);
        System.out.println("ConfigProperties注入: " + (configProperties != null ? "✅" : "❌"));

        if (configProperties != null) {
            System.out.println("upload-path: " + configProperties.getUploadPath());
            System.out.println("upload-location: " + configProperties.getUploadLocation());
            System.out.println("server-url: " + configProperties.getServerUrl());
        }

        System.out.println("=== 配置属性测试完成 ===");
    }
}
