//! 设备服务配置管理
//!
//! 从环境变量加载配置，回退到默认值。

/// 设备服务配置
pub struct Config {
    pub host: String,
    pub tcp_port: u16,
    pub node_id: String,
    pub redis_url: String,
    pub heartbeat_interval: u64,
    pub register_ttl: u64,
}

impl Config {
    /// 从环境变量加载配置
    pub fn from_env() -> Self {
        let _ = dotenvy::dotenv();

        Self {
            host: std::env::var("HOST")
                .unwrap_or_else(|_| "0.0.0.0".to_string()),
            tcp_port: std::env::var("TCP_PORT")
                .unwrap_or_else(|_| "3002".to_string())
                .parse()
                .expect("TCP_PORT must be a valid number"),
            node_id: std::env::var("NODE_ID")
                .unwrap_or_else(|_| "node-1".to_string()),
            redis_url: std::env::var("REDIS_URL")
                .expect("REDIS_URL must be set"),
            heartbeat_interval: std::env::var("HEARTBEAT_INTERVAL")
                .unwrap_or_else(|_| "60".to_string())
                .parse()
                .expect("HEARTBEAT_INTERVAL must be a valid number"),
            register_ttl: std::env::var("REGISTER_TTL")
                .unwrap_or_else(|_| "300".to_string())
                .parse()
                .expect("REGISTER_TTL must be a valid number"),
        }
    }
}
