//! RouteMetadataAccess trait implementation for PageRouteInfo
//!
//! This provides the implementation of RouteMetadataAccess for the local
//! PageRouteInfo struct

use crate::route_analysis::PageRouteInfo;
use crate::utils::RouteMetadataAccess;

/// Implementation of RouteMetadataAccess for PageRouteInfo (from
/// route_analysis) This bridges the gap between the structured PageRouteInfo
/// and metadata-based access
impl RouteMetadataAccess for PageRouteInfo {
    fn get_language(&self) -> Option<String> {
        Some(self.language.clone())
    }

    fn get_title_key(&self) -> Option<String> {
        Some(self.title_key.clone())
    }

    fn get_description_key(&self) -> Option<String> {
        self.description_key.clone()
    }

    fn get_enabled(&self) -> bool {
        self.enabled
    }

    fn get_priority(&self) -> f32 {
        self.priority as f32
    }

    fn get_keywords(&self) -> Option<String> {
        if self.keywords.is_empty() {
            None
        } else {
            Some(self.keywords.join(", "))
        }
    }

    fn get_menu_group(&self) -> Option<String> {
        self.menu_group.clone()
    }

    fn get_menu_order(&self) -> Option<i32> {
        self.menu_order
    }

    fn get_menu_icon(&self) -> Option<String> {
        self.menu_icon.clone()
    }

    fn get_is_external(&self) -> bool {
        false // PageRouteInfo doesn't have this field, default to false
    }

    fn get_requires_auth(&self) -> bool {
        self.requires_auth.unwrap_or(false)
    }

    fn get_show_in_sitemap(&self) -> bool {
        true // PageRouteInfo doesn't have this field, default to true
    }

    fn get_canonical_path(&self) -> Option<String> {
        None // PageRouteInfo doesn't have this field
    }

    fn get_unified_component(&self) -> Option<String> {
        Some(self.unified_component.clone())
    }

    fn get_template(&self) -> Option<String> {
        None // PageRouteInfo doesn't have this field
    }

    fn set_metadata(&mut self, key: &str, value: &str) {
        // For PageRouteInfo, we can only set known fields
        match key {
            "language" => self.language = value.to_string(),
            "title_key" => self.title_key = value.to_string(),
            "description_key" => self.description_key = Some(value.to_string()),
            "enabled" => self.enabled = value.parse().unwrap_or(true),
            "priority" => self.priority = value.parse().unwrap_or(1.0),
            "requires_auth" => self.requires_auth = Some(value.parse().unwrap_or(false)),
            "menu_group" => self.menu_group = Some(value.to_string()),
            "menu_order" => self.menu_order = value.parse().ok(),
            "menu_icon" => self.menu_icon = Some(value.to_string()),
            "unified_component" => self.unified_component = value.to_string(),
            _ => {} // Ignore unknown fields
        }
    }

    fn get_metadata(&self, key: &str) -> Option<String> {
        match key {
            "language" => Some(self.language.clone()),
            "title_key" => Some(self.title_key.clone()),
            "description_key" => self.description_key.clone(),
            "enabled" => Some(self.enabled.to_string()),
            "priority" => Some(self.priority.to_string()),
            "requires_auth" => self.requires_auth.map(|b| b.to_string()),
            "menu_group" => self.menu_group.clone(),
            "menu_order" => self.menu_order.map(|n| n.to_string()),
            "menu_icon" => self.menu_icon.clone(),
            "unified_component" => Some(self.unified_component.clone()),
            _ => None,
        }
    }

    fn get_content_type(&self) -> Option<String> {
        // PageRouteInfo doesn't have a specific content_type field
        // Could infer from path or component, but return None for now
        None
    }

    fn get_metadata_keys(&self) -> Vec<String> {
        // Return all the available metadata keys from PageRouteInfo
        vec![
            "language".to_string(),
            "title_key".to_string(),
            "description_key".to_string(),
            "enabled".to_string(),
            "priority".to_string(),
            "requires_auth".to_string(),
            "menu_group".to_string(),
            "menu_order".to_string(),
            "menu_icon".to_string(),
            "unified_component".to_string(),
        ]
    }

    fn get_all_metadata(&self) -> std::collections::HashMap<String, String> {
        let mut metadata = std::collections::HashMap::new();
        metadata.insert("language".to_string(), self.language.clone());
        metadata.insert("title_key".to_string(), self.title_key.clone());
        if let Some(ref desc) = self.description_key {
            metadata.insert("description_key".to_string(), desc.clone());
        }
        metadata.insert("enabled".to_string(), self.enabled.to_string());
        metadata.insert("priority".to_string(), self.priority.to_string());
        if let Some(auth) = self.requires_auth {
            metadata.insert("requires_auth".to_string(), auth.to_string());
        }
        if let Some(ref group) = self.menu_group {
            metadata.insert("menu_group".to_string(), group.clone());
        }
        if let Some(order) = self.menu_order {
            metadata.insert("menu_order".to_string(), order.to_string());
        }
        if let Some(ref icon) = self.menu_icon {
            metadata.insert("menu_icon".to_string(), icon.clone());
        }
        metadata.insert(
            "unified_component".to_string(),
            self.unified_component.clone(),
        );
        metadata
    }

    fn has_metadata(&self, key: &str) -> bool {
        match key {
            "language" | "title_key" | "enabled" | "priority" | "unified_component" => true,
            "description_key" => self.description_key.is_some(),
            "requires_auth" => self.requires_auth.is_some(),
            "menu_group" => self.menu_group.is_some(),
            "menu_order" => self.menu_order.is_some(),
            "menu_icon" => self.menu_icon.is_some(),
            _ => false,
        }
    }

    fn get_auto_generate(&self) -> bool {
        false // PageRouteInfo doesn't have auto_generate field, default to
              // false
    }

    fn get_i18n_patterns(&self) -> Vec<String> {
        // Default pattern based on component or title_key
        vec![format!("{}-", self.title_key.to_lowercase())]
    }

    fn get_template_params(&self) -> toml::Value {
        toml::Value::Table(toml::map::Map::new())
    }
}
