#[derive(Clone)]
pub struct AppConfig {
    pub allow_registration: bool,
    pub base_url: String,
    pub rate_limit: u64,
    pub refresh_ttl_seconds: u64,
    pub wrapup: WrapUpConfig,
}

#[derive(Clone)]
pub struct WrapUpConfig {
    pub font_path: Option<String>,
    pub logo_path: Option<String>,
    pub bg_dir: Option<String>,
}

impl AppConfig {
    pub fn from_env() -> Self {
        let allow_registration = std::env::var("ALLOW_REGISTRATION")
            .map(|v| v == "true" || v == "1")
            .unwrap_or(false);
        let base_url = std::env::var("BASE_URL")
            .unwrap_or_else(|_| "http://localhost:3000".to_string())
            .trim_end_matches('/')
            .to_string();
        let rate_limit = std::env::var("RATE_LIMIT")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(60);
        let refresh_ttl_seconds = std::env::var("REFRESH_TTL_SECONDS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(2_592_000u64);
        Self {
            allow_registration,
            base_url,
            rate_limit,
            refresh_ttl_seconds,
            wrapup: WrapUpConfig::from_env(),
        }
    }
}

impl WrapUpConfig {
    pub fn from_env() -> Self {
        Self {
            font_path: std::env::var("WRAPUP_FONT_PATH").ok(),
            logo_path: std::env::var("WRAPUP_LOGO_PATH").ok(),
            bg_dir: std::env::var("WRAPUP_BG_DIR").ok(),
        }
    }
}
