package top.gtb520.java.pve_back_api.config;

/**
 * 全局配置管理类
 * 将配置文件中的配置项存储到全局静态变量中
 */
public class AppConfig {
    
    // 全局配置
    public static volatile String HTTP_IP = "0.0.0.0";
    public static volatile String HTTP_PORT = "8080";
    
    // 数据库配置
    public static volatile String MYSQL_JDBC = "";
    public static volatile String MYSQL_USERNAME = "";
    public static volatile String MYSQL_PASSWORD = "";
    
    // PVE配置 - 保留原有URL并新增拆解配置
    public static volatile String PVE_URL = "";
    public static volatile String PVE_HOSTNAME_IP = "";
    public static volatile String PVE_PORT = "8006";
    public static volatile String PVE_PROTOCOL = "https";
    public static volatile String PVE_API_TOKEN_NAME = "";
    public static volatile String PVE_API_TOKEN_ID = "";
    public static volatile String PVE_API_TOKEN_SECRET = "";
    
    // 配置状态
    private static volatile boolean isLoaded = false;
    
    private AppConfig() {
        // 私有构造函数，防止实例化
    }
    
    /**
     * 标记配置已加载
     */
    public static void markAsLoaded() {
        isLoaded = true;
    }
    
    /**
     * 检查配置是否已加载
     */
    public static boolean isConfigLoaded() {
        return isLoaded;
    }
    
    /**
     * 获取完整的PVE URL（优先使用拆解后的配置构建）
     */
    public static String getPveFullUrl() {
        // 如果有拆解后的配置，优先使用它们构建URL
        if (PVE_HOSTNAME_IP != null && !PVE_HOSTNAME_IP.isEmpty()) {
            return PVE_PROTOCOL + "://" + PVE_HOSTNAME_IP + ":" + PVE_PORT + "/";
        }
        // 否则返回原始URL
        return PVE_URL != null ? PVE_URL : "";
    }
    
    /**
     * 获取完整的数据库连接URL（包含用户名和密码）
     */
    public static String getFullJdbcUrl() {
        if (MYSQL_JDBC == null || MYSQL_JDBC.isEmpty()) {
            return "";
        }
        return MYSQL_JDBC + "?user=" + MYSQL_USERNAME + "&password=" + MYSQL_PASSWORD;
    }
    
    /**
     * 验证必要配置是否完整
     */
    public static boolean validateRequiredConfig() {
        return HTTP_IP != null && !HTTP_IP.isEmpty() &&
               HTTP_PORT != null && !HTTP_PORT.isEmpty() &&
               (PVE_URL != null && !PVE_URL.isEmpty()) || 
               (PVE_HOSTNAME_IP != null && !PVE_HOSTNAME_IP.isEmpty());
    }
    
    /**
     * 打印当前配置信息
     */
    public static void printCurrentConfig() {
        System.out.println("=== 应用配置信息 ===");
        System.out.println("HTTP 监听地址: " + HTTP_IP + ":" + HTTP_PORT);
        System.out.println("MySQL 连接: " + (MYSQL_JDBC != null ? "已配置" : "未配置"));
        System.out.println("PVE 原始URL: " + (PVE_URL != null ? PVE_URL : "未配置"));
        System.out.println("PVE 主机: " + (PVE_HOSTNAME_IP != null ? PVE_HOSTNAME_IP : "未配置"));
        System.out.println("PVE 端口: " + PVE_PORT);
        System.out.println("PVE 协议: " + PVE_PROTOCOL);
        System.out.println("配置加载状态: " + (isLoaded ? "已完成" : "未完成"));
        System.out.println("==================");
    }
    
    /**
     * 重置配置到默认值
     */
    public static void resetToDefaults() {
        HTTP_IP = "0.0.0.0";
        HTTP_PORT = "8080";
        MYSQL_JDBC = "";
        MYSQL_USERNAME = "";
        MYSQL_PASSWORD = "";
        PVE_URL = "";
        PVE_HOSTNAME_IP = "";
        PVE_PORT = "8006";
        PVE_PROTOCOL = "https";
        PVE_API_TOKEN_NAME = "";
        PVE_API_TOKEN_ID = "";
        PVE_API_TOKEN_SECRET = "";
        isLoaded = false;
    }
}
