use std::env;
use dotenvy::dotenv;
use tracing;

#[derive(Debug, Clone)]
pub struct Config {
    pub database_url: String,
    pub jwt_secret: String,
    pub wechat_appid: String,
    pub wechat_secret: String,
    pub server_ports: Vec<u16>,
}

impl Config {
    pub fn load() -> Result<Self, String> {
        // 加载.env文件
        if let Err(e) = dotenv() {
            tracing::warn!("无法加载.env文件 - {}", e);
        }

        Ok(Self {
            database_url: env::var("DATABASE_URL")
                .map_err(|_| "环境变量DATABASE_URL未设置".to_string())?,
            jwt_secret: env::var("JWT_SECRET")
                .map_err(|_| "环境变量JWT_SECRET未设置".to_string())?,
            wechat_appid: env::var("WECHAT_APPID")
                .map_err(|_| "环境变量WECHAT_APPID未设置".to_string())?,
            wechat_secret: env::var("WECHAT_SECRET")
                .map_err(|_| "环境变量WECHAT_SECRET未设置".to_string())?,
            server_ports: vec![8080, 3000, 8000, 8888],
        })
    }
}