use config::AppConfig;
use tracing_subscriber::EnvFilter;

pub struct AppContext {
    pub config: AppConfig,
}

impl AppContext {
    pub fn init() -> anyhow::Result<Self> {
        let config = AppConfig::load();
        init_tracing(&config)?;
        Ok(Self { config })
    }
}

fn init_tracing(config: &AppConfig) -> anyhow::Result<()> {
    let level = config.log_level()?;
    let filter = EnvFilter::try_new(&level)?;
    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_target(false)
        .init();
    Ok(())
}
