use config::Config;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct AppConfig {
    pub server_port: u16,
    pub nats_addr: String,
    pub data_persistence_service_url: String,
    pub report_generator_service_url: String,
    pub workflow_orchestrator_service_url: String,
}

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

        let server_port: u16 = cfg.get::<u16>("server_port")?;
        let nats_addr: String = cfg.get::<String>("nats_addr")?;
        let data_persistence_service_url: String =
            cfg.get::<String>("data_persistence_service_url")?;
        let report_generator_service_url: String = cfg
            .get::<String>("report_generator_service_url")
            .unwrap_or_else(|_| "http://report-generator-service:8004".to_string());
        let workflow_orchestrator_service_url: String = cfg
            .get::<String>("workflow_orchestrator_service_url")
            .unwrap_or_else(|_| "http://workflow-orchestrator-service:8005".to_string());

        Ok(Self {
            server_port,
            nats_addr,
            data_persistence_service_url,
            report_generator_service_url,
            workflow_orchestrator_service_url,
        })
    }
}
