//! Configuration utilities

use crate::*;

/// Application configuration
#[derive(Debug, Clone)]
pub struct AppConfig {
    /// Server configuration
    pub server: ServerConfig,
    /// Site configuration
    pub site: SiteConfig,
    /// Framework configuration
    pub framework: FrameworkConfig,
}

impl AppConfig {
    /// Load configuration from environment and defaults
    pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
        let server = ServerConfig::from_env()?;

        let site = SiteConfig {
            name: std::env::var("SITE_NAME")
                .unwrap_or_else(|_| "Rustelo Site".to_string()),
            description: std::env::var("SITE_DESCRIPTION")
                .unwrap_or_else(|_| "A Rustelo web application".to_string()),
            base_url: std::env::var("SITE_BASE_URL")
                .unwrap_or_else(|_| format!("http://{}", server.addr)),
            default_language: std::env::var("DEFAULT_LANGUAGE")
                .unwrap_or_else(|_| "en".to_string()),
            languages: vec![], // Implementation should load from config files
            default_theme: std::env::var("DEFAULT_THEME")
                .unwrap_or_else(|_| "default".to_string()),
            themes: vec![], // Implementation should load from config files
            contact_email: std::env::var("CONTACT_EMAIL").ok(),
            metadata: std::collections::HashMap::new(),
        };

        let framework = FrameworkConfig {
            environment: server.environment.clone(),
            debug_enabled: matches!(server.environment, Environment::Development),
            hot_reload: matches!(server.environment, Environment::Development),
            cache_config: CacheConfig::default(),
            security_config: SecurityConfig::default(),
        };

        Ok(Self {
            server,
            site,
            framework,
        })
    }
}