//! Type definitions for route generation

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Route configuration structure - mirrors shared::routing::rustelo_components_leptos::RouteConfigToml
/// This must stay in sync with the shared version to ensure compatibility
#[derive(Deserialize, Serialize, Debug)]
#[allow(dead_code)]
pub struct Route {
    pub path: String,
    pub component: String,
    pub title_key: String,
    pub language: String,
    pub enabled: bool,
    #[serde(default)]
    pub description_key: Option<String>,
    #[serde(default)]
    pub keywords: Option<Vec<String>>,
    #[serde(default)]
    pub priority: Option<f32>,
    #[serde(default)]
    pub menu_group: Option<String>,
    #[serde(default)]
    pub menu_order: Option<i32>,
    #[serde(default)]
    pub menu_icon: Option<String>,
    #[serde(default)]
    pub is_external: Option<bool>,
    #[serde(default)]
    pub requires_auth: Option<bool>,
    #[serde(default)]
    pub show_in_sitemap: Option<bool>,
    #[serde(default)]
    pub alternate_paths: Option<Vec<String>>,
    #[serde(default)]
    pub canonical_path: Option<String>,
    #[serde(default)]
    pub content_type: Option<String>,
    #[serde(default)]
    pub props: Option<HashMap<String, toml::Value>>,

    // Enhanced fields for unified route system
    #[serde(default)]
    pub page_component: Option<String>,
    #[serde(default)]
    pub parameter_extraction: Option<HashMap<String, String>>,
    #[serde(default)]
    pub fallback_component: Option<String>,

    // Template system fields
    #[serde(default)]
    pub template: Option<String>,
    #[serde(default)]
    pub i18n_patterns: Option<Vec<String>>,
    #[serde(default)]
    pub auto_generate: Option<bool>,
    #[serde(default)]
    pub template_params: Option<HashMap<String, toml::Value>>,

    // Build-time generation fields
    #[serde(default)]
    pub unified_component: Option<String>,
    #[serde(default)]
    pub generate_boilerplate_only: Option<bool>,
    #[serde(default)]
    pub content_type_param: Option<bool>,
    #[serde(default)]
    pub category_param: Option<bool>,
    #[serde(default)]
    pub slug_param: Option<bool>,
    #[serde(default)]
    pub replace_existing_boilerplate: Option<bool>,

    // Auto-scaffolding fields
    #[serde(default)]
    pub auto_scaffold: Option<bool>,
    #[serde(default)]
    pub scaffold_template: Option<String>,
    #[serde(default)]
    pub module_path: Option<String>,
    #[serde(default)]
    pub delete_existing_page: Option<bool>,

    // NEW UNIFIED FIELDS to match shared RouteConfigToml
    #[serde(default)]
    pub lang_prefixes: Option<Vec<String>>, // Language prefixes for i18n
    #[serde(default)]
    pub params_component: Option<HashMap<String, String>>, // Component parameters

    // GENERIC COMPONENT CONFIGURATION FIELDS
    #[serde(default)]
    pub component_dynamic: Option<bool>, // Controls dynamic content_type detection
    #[serde(default)]
    pub component_path: Option<String>, // Controls component directory location ("admin", "")
    #[serde(default)]
    pub component_prefix: Option<String>, // Controls component name prefix removal ("Admin")
}

impl Route {
    /// Get the unified component name, fallback to component if not specified
    pub fn get_unified_component(&self) -> String {
        self.unified_component
            .as_ref()
            .unwrap_or(&self.component)
            .clone()
    }

    /// Get language prefixes for i18n, defaulting to empty vec if not specified
    pub fn get_lang_prefixes(&self) -> Vec<String> {
        self.lang_prefixes.as_ref().unwrap_or(&Vec::new()).clone()
    }

    /// Get component parameters, defaulting to empty map if not specified
    pub fn get_params_component(&self) -> HashMap<String, String> {
        self.params_component
            .as_ref()
            .unwrap_or(&HashMap::new())
            .clone()
    }

    /// Get content_type from direct field or props section
    pub fn get_content_type(&self) -> Option<String> {
        // First try direct content_type field
        if let Some(ref content_type) = self.content_type {
            return Some(content_type.clone());
        }

        // Fallback to props.content_type
        if let Some(ref props) = self.props {
            if let Some(content_type) = props.get("content_type") {
                if let Some(content_type_str) = content_type.as_str() {
                    return Some(content_type_str.to_string());
                }
            }
        }

        None
    }
}

/// Routes configuration container
#[derive(Deserialize, Serialize, Debug)]
pub struct RoutesConfig {
    pub routes: Vec<Route>,
}

/// Component information for route generation
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ComponentInfo {
    pub name: String,
    pub key_prefix: String,
    pub page_component: Option<String>,
    pub unified_component: String,
    pub lang_prefixes: Vec<String>,
    pub params_component: HashMap<String, String>,
    pub props: Option<HashMap<String, toml::Value>>,
    pub parameter_extraction: Option<HashMap<String, String>>,
    pub fallback_component: Option<String>,
    pub content_type: Option<String>,

    // GENERIC COMPONENT CONFIGURATION FIELDS
    pub component_dynamic: bool,      // Controls dynamic content_type detection
    pub component_path: String,       // Controls component directory location
    pub component_prefix: String,     // Controls component name prefix removal
}
