use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
#[allow(dead_code)]
pub struct AppConfig {
    pub server_port: u16,
    pub nats_addr: String,
    pub data_persistence_service_url: String,
    pub alphavantage_api_key: Option<String>,
    
    // New fields
    pub api_gateway_url: String,
    pub service_host: String,
}

impl AppConfig {
    #[allow(dead_code)]
    pub fn load() -> Result<Self, config::ConfigError> {
        let config = config::Config::builder()
            .add_source(config::Environment::default().separator("__"))
            .build()?;

        let cfg: AppConfig = config.try_deserialize()?;

        if cfg.data_persistence_service_url.trim().is_empty() {
            return Err(config::ConfigError::Message(
                "DATA_PERSISTENCE_SERVICE_URL must not be empty".to_string(),
            ));
        }
        if cfg.api_gateway_url.trim().is_empty() {
            return Err(config::ConfigError::Message(
                "API_GATEWAY_URL must not be empty".to_string(),
            ));
        }
        if cfg.service_host.trim().is_empty() {
            return Err(config::ConfigError::Message(
                "SERVICE_HOST must not be empty".to_string(),
            ));
        }

        Ok(cfg)
    }
}
