//! FTL (Fluent) translations management module - language agnostic

use std::collections::HashMap;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

/// Container for page-specific translation keys
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FtlKeys {
    pub pages: PageKeys,
    pub components: ComponentKeys,
}

impl FtlKeys {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_discovered_languages(available_languages: &[String]) -> Self {
        Self {
            pages: PageKeys::from_discovered_languages(available_languages),
            components: ComponentKeys::from_discovered_languages(available_languages),
        }
    }
}

/// Modular page key structures for easy management
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PageKeys {
    // Core navigation pages
    pub home: HashMap<String, Vec<String>>,
    pub about: HashMap<String, Vec<String>>,
    pub contact: HashMap<String, Vec<String>>,
    pub legal: HashMap<String, Vec<String>>,
    pub privacy: HashMap<String, Vec<String>>,
}

impl PageKeys {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_discovered_languages(available_languages: &[String]) -> Self {
        let mut pages = Self::default();

        // Define page keys that are language-agnostic (same keys for all languages)
        let home_keys: Vec<String> = Vec::new();
        // vec![].into_iter().map(|s| s.to_string()).collect();

        // let contact_keys: Vec<String> = Vec::new();
        // vec![].into_iter().map(|s| s.to_string()).collect();

        // Apply same keys to all available languages
        for lang in available_languages {
            pages.home.insert(lang.clone(), home_keys.clone());
            //    pages.contact.insert(lang.clone(), contact_keys.clone());
        }

        pages
    }

    /// Check if a page is enabled based on available translations
    pub fn is_page_enabled(&self, page: &str, lang: &str) -> bool {
        self.get_page_hashmap(page)
            .map(|hashmap| hashmap.contains_key(lang))
            .unwrap_or(false)
    }

    /// Get the HashMap for a specific page
    fn get_page_hashmap(&self, page: &str) -> Option<&HashMap<String, Vec<String>>> {
        match page {
            "home" => Some(&self.home),
            "contact" => Some(&self.contact),
            "about" => Some(&self.about),
            "legal" => Some(&self.legal),
            "privacy" => Some(&self.privacy),
            _ => None,
        }
    }

    /// Load keys for a specific page
    pub fn load_page_keys(&self, page: &str, lang: &str) -> Vec<String> {
        self.get_page_hashmap(page)
            .and_then(|hashmap| hashmap.get(lang))
            .cloned()
            .unwrap_or_default()
    }

    /// Enable/disable entire pages by clearing their translations
    pub fn disable_page(&mut self, page: &str) {
        if let Some(hashmap) = self.get_page_hashmap_mut(page) {
            hashmap.clear();
        }
    }

    /// Get the mutable HashMap for a specific page
    fn get_page_hashmap_mut(&mut self, page: &str) -> Option<&mut HashMap<String, Vec<String>>> {
        match page {
            "home" => Some(&mut self.home),
            "contact" => Some(&mut self.contact),
            "about" => Some(&mut self.about),
            "legal" => Some(&mut self.legal),
            "privacy" => Some(&mut self.privacy),
            _ => None,
        }
    }

    /// Get all available languages for this page structure
    pub fn get_available_languages(&self) -> Vec<String> {
        let mut languages = std::collections::HashSet::new();

        // Collect languages from all page maps
        for hashmap in [
            &self.home,
            &self.contact,
            &self.about,
            &self.legal,
            &self.privacy,
        ] {
            for lang in hashmap.keys() {
                languages.insert(lang.clone());
            }
        }

        let mut lang_vec: Vec<String> = languages.into_iter().collect();
        lang_vec.sort();
        lang_vec
    }
}

/// Component-specific key structures
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ComponentKeys {
    // Global components
    pub navigation: HashMap<String, Vec<String>>,
    pub footer: HashMap<String, Vec<String>>,
    pub forms: HashMap<String, Vec<String>>,
    pub content: HashMap<String, Vec<String>>,
}

impl ComponentKeys {
    /// Get all available languages for this component structure
    pub fn get_available_languages(&self) -> Vec<String> {
        let mut languages = std::collections::HashSet::new();

        // Collect languages from all component maps
        for hashmap in [&self.navigation, &self.footer, &self.forms, &self.content] {
            for lang in hashmap.keys() {
                languages.insert(lang.clone());
            }
        }

        let mut lang_vec: Vec<String> = languages.into_iter().collect();
        lang_vec.sort();
        lang_vec
    }
}

impl ComponentKeys {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_discovered_languages(available_languages: &[String]) -> Self {
        let mut components = Self::default();

        // Define component keys that are language-agnostic
        let navigation_keys: Vec<String> = vec!["home", "about", "contact"]
            .into_iter()
            .map(|s| s.to_string())
            .collect();

        let footer_keys: Vec<String> = vec![
            "pages",
            "copyright-name",
            "scroll-to-top",
            "privacy",
            "legal",
        ]
        .into_iter()
        .map(|s| s.to_string())
        .collect();

        // Apply same keys to all available languages
        for lang in available_languages {
            components
                .navigation
                .insert(lang.clone(), navigation_keys.clone());
            components.footer.insert(lang.clone(), footer_keys.clone());
        }

        components
    }
}
