use std::env;
use anyhow::{Result, Context};
use serde::Deserialize;

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

impl AppConfig {
    pub fn load() -> Result<Self> {
        let nats_addr = env::var("NATS_ADDR").unwrap_or_else(|_| "nats://nats:4222".to_string());
        let server_port = env::var("SERVER_PORT")
            .unwrap_or_else(|_| "8005".to_string())
            .parse()
            .context("SERVER_PORT must be a number")?;
        let data_persistence_service_url = env::var("DATA_PERSISTENCE_SERVICE_URL")
            .unwrap_or_else(|_| "http://data-persistence-service:3000/api/v1".to_string());

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

