//! Internationalization (i18n) types and language management

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

// Re-use LanguageCode from routing
use crate::routing::LanguageCode;

/// Text direction for languages
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TextDirection {
    /// Left-to-right (most languages)
    LeftToRight,
    /// Right-to-left (Arabic, Hebrew, etc.)
    RightToLeft,
}

impl std::fmt::Display for TextDirection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TextDirection::LeftToRight => write!(f, "ltr"),
            TextDirection::RightToLeft => write!(f, "rtl"),
        }
    }
}

/// Language configuration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LanguageConfig {
    /// Language code (ISO 639-1)
    pub code: LanguageCode,
    /// Native language name
    pub name: String,
    /// English language name
    pub english_name: String,
    /// Text direction (ltr/rtl)
    pub direction: TextDirection,
    /// Whether this is the default language
    pub is_default: bool,
    /// Whether language is enabled
    pub is_enabled: bool,
    /// URL prefix for this language (empty for default)
    pub url_prefix: String,
}

/// Language information for i18n
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LanguageInfo {
    /// Language code (ISO 639-1)
    pub code: LanguageCode,
    /// Language name in native script
    pub name: String,
    /// Language name in English
    pub english_name: String,
    /// Text direction
    pub direction: TextDirection,
    /// Available locales for this language
    pub available_locales: Vec<String>,
}

/// Registry for available languages
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LanguageRegistry {
    /// Available languages
    pub languages: HashMap<LanguageCode, LanguageInfo>,
    /// Default language code
    pub default_language: LanguageCode,
}

impl LanguageRegistry {
    /// Create new language registry
    pub fn new() -> Self {
        Self {
            languages: HashMap::new(),
            default_language: "en".to_string(),
        }
    }

    /// Register a new language
    pub fn register(&mut self, code: LanguageCode, info: LanguageInfo) {
        self.languages.insert(code, info);
    }

    /// Get available language codes
    pub fn available_languages(&self) -> Vec<&LanguageCode> {
        self.languages.keys().collect()
    }
}

impl Default for LanguageRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Get current language - simplified version for tools
pub fn get_current_language() -> Option<String> {
    // Simplified version that doesn't depend on generated code
    // Tools can use this basic version, runtime can use the full version
    Some("en".to_string())
}

/// Discover available languages - simplified version for tools
pub fn discover_available_languages() -> Vec<LanguageInfo> {
    // Simplified version that returns basic English support
    vec![LanguageInfo {
        code: "en".to_string(),
        name: "English".to_string(),
        english_name: "English".to_string(),
        direction: TextDirection::LeftToRight,
        available_locales: vec!["en".to_string()],
    }]
}

/// Get default language - simplified version for tools
pub fn get_default_language() -> String {
    "en".to_string()
}

/// Basic content provider creation - simplified version
pub fn create_content_provider() -> Result<(), String> {
    // Placeholder implementation for tools
    Ok(())
}
