//! Central language configuration system
//!
//! This module provides a single source of truth for language configuration,
//! now integrated with filesystem-based language discovery.

use std::sync::LazyLock;

/// Configuration for language settings
#[derive(Debug, Clone)]
pub struct LanguageConfig {
    /// The default language code (e.g., "en", "es", "fr")
    pub default_language: String,
    /// List of supported languages
    pub supported_languages: Vec<String>,
}

impl Default for LanguageConfig {
    fn default() -> Self {
        // Use dynamic language discovery for defaults instead of hardcoded languages
        let discovered_languages = crate::i18n::discovery::discover_available_languages();
        let default_language = crate::i18n::discovery::get_default_language();

        Self {
            default_language: default_language.to_string(),
            supported_languages: discovered_languages.clone(),
        }
    }
}

/// Global language configuration instance - now integrates with filesystem
/// discovery
static LANGUAGE_CONFIG: LazyLock<LanguageConfig> = LazyLock::new(|| {
    // Primary: Use filesystem discovery from content/locales
    let discovered_languages = crate::i18n::discovery::discover_available_languages();
    let default_from_discovery = crate::i18n::discovery::get_default_language();

    // Secondary: Environment variable overrides (if language is discovered)
    let default_lang = std::env::var("DEFAULT_LANGUAGE")
        .ok()
        .filter(|lang| discovered_languages.contains(lang))
        .unwrap_or_else(|| default_from_discovery.to_string());

    let supported = if let Ok(env_supported) = std::env::var("SUPPORTED_LANGUAGES") {
        // Filter environment languages to only include discovered ones
        let env_langs: Vec<String> = env_supported
            .split(',')
            .map(|s| s.trim().to_string())
            .filter(|lang| discovered_languages.contains(lang))
            .collect();

        if env_langs.is_empty() {
            // If no environment languages are valid, use discovered languages
            discovered_languages.clone()
        } else {
            env_langs
        }
    } else {
        // Use discovered languages as primary source
        discovered_languages.clone()
    };

    LanguageConfig {
        default_language: default_lang,
        supported_languages: supported,
    }
});

/// Get the default language code
pub fn get_default_language() -> &'static str {
    &LANGUAGE_CONFIG.default_language
}

/// Get the list of supported languages
pub fn get_supported_languages() -> &'static Vec<String> {
    &LANGUAGE_CONFIG.supported_languages
}

/// Check if a language is supported
pub fn is_supported_language(lang: &str) -> bool {
    LANGUAGE_CONFIG
        .supported_languages
        .contains(&lang.to_string())
}

/// Get language config for testing or advanced use cases
pub fn get_language_config() -> &'static LanguageConfig {
    &LANGUAGE_CONFIG
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_language() {
        let default = get_default_language();
        assert!(!default.is_empty());
        assert!(is_supported_language(default));
    }

    #[test]
    fn test_supported_languages() {
        let languages = get_supported_languages();
        assert!(!languages.is_empty());
        // Test that the default language is always supported
        let default = get_default_language();
        assert!(languages.contains(&default.to_string()));
    }

    #[test]
    fn test_language_support_check() {
        let default_lang = get_default_language();
        assert!(is_supported_language(default_lang));

        // Test that a non-existent language is not supported
        assert!(!is_supported_language("xyz-nonexistent"));
    }
}
