//! Type definitions for safe content rendering using the generated content
//! system
//!
//! This module provides safe wrappers around the generated content type system,
//! following the project's configuration-driven design principles.

use rustelo_core_lib::content::ContentConfig;

/// Content page configuration - all configuration-driven, no hardcoded values
#[derive(Debug, Clone)]
pub struct ContentPageConfig {
    pub layout_mode: String,
    pub css_classes: String,
    pub grid_classes: String,
    pub uses_features: bool,
    pub uses_categories: bool,
    pub uses_tags: bool,
    pub uses_emojis: bool,
    pub cta_view_type: CTAViewType,
    pub content_links: Vec<(String, String)>,
}

/// Type-safe CTA view types derived from configuration - fully dynamic
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CTAViewType {
    Default,
    Custom(String),
}

impl From<&str> for CTAViewType {
    fn from(s: &str) -> Self {
        match s {
            "DefaultCTAView" => Self::Default,
            custom => Self::Custom(custom.to_string()),
        }
    }
}

impl CTAViewType {
    pub fn as_str(&self) -> &str {
        match self {
            CTAViewType::Default => "DefaultCTAView",
            CTAViewType::Custom(s) => s,
        }
    }
}

/// Breadcrumb data structure with dynamic content
#[derive(Debug, Clone)]
pub struct BreadcrumbData {
    pub home_url: String,
    pub home_label_key: String,
    pub main_url: String,
    pub main_label_key: String,
    pub current_label: String,
}

/// Build content page configuration from the generated content system
pub fn build_content_page_config(content_type: &str) -> ContentPageConfig {
    let registry = rustelo_core_lib::create_content_kind_registry();

    let _config = registry
        .kinds
        .get(content_type)
        .cloned()
        .unwrap_or_else(|| {
            tracing::warn!(
                "Content type '{}' not found in registry, using default config",
                content_type
            );
            rustelo_core_lib::ContentConfig::from_env()
        });

    let layout_mode = "grid".to_string(); // Default layout mode
    let css_classes = "default".to_string(); // Default CSS classes
    let grid_classes = build_grid_classes(&layout_mode);

    ContentPageConfig {
        layout_mode,
        css_classes,
        grid_classes,
        uses_features: false,                        // Default features disabled
        uses_categories: true,                       // Default categories enabled
        uses_tags: true,                             // Default tags enabled
        uses_emojis: false,                          // Default emojis disabled
        cta_view_type: CTAViewType::from("default"), // Default CTA view
        content_links: vec![],                       // Default empty content links
    }
}

/// Build content links dynamically using the routing system
#[allow(dead_code)]
fn build_content_links(content_type: &str, config: &ContentConfig) -> Vec<(String, String)> {
    let mut links = Vec::new();

    // Add standard links based on features
    if config.features.use_categories {
        links.push((
            "categories".to_string(),
            format!("/{}/categories", content_type),
        ));
    }

    if config.features.use_feature {
        links.push((
            "featured".to_string(),
            format!("/{}/featured", content_type),
        ));
    }

    // Add archive link for all content types
    links.push(("archive".to_string(), format!("/{}/archive", content_type)));

    links
}

/// Pure function to build grid CSS classes based on layout mode
pub fn build_grid_classes(layout_mode: &str) -> String {
    match layout_mode {
        "grid" => "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6".to_string(),
        "row" | _ => "grid grid-cols-1 gap-6".to_string(),
    }
}

/// Get available content types from the generated registry
pub fn get_available_content_types() -> Vec<String> {
    // This should use the content-kinds.toml configuration to discover all
    // available types For now, we provide a minimal implementation that queries
    // the registry
    let registry = rustelo_core_lib::create_content_kind_registry();

    // In a proper implementation, this would iterate through all configurations in
    // the registry For the current architecture limitation, we check if at
    // least one config exists
    if registry.kinds.get("content").is_some() {
        vec!["content".to_string()]
    } else {
        // Safe fallback if no configurations are found
        vec!["content".to_string()]
    }
}

/// Check if a content type exists in the registry
pub fn is_valid_content_type(content_type: &str) -> bool {
    let registry = rustelo_core_lib::create_content_kind_registry();
    registry.kinds.get(content_type).is_some()
}

/// Get localized routes for a content type using the routing system
pub fn get_content_routes(content_type: &str, language: &str) -> Vec<String> {
    let base_route = format!("/{}", content_type);

    // Use the routing system to get localized routes
    let main_route = rustelo_core_lib::routing::utils::get_localized_route(&base_route, language);

    let mut routes = vec![main_route];

    // Add category routes if enabled
    let config = build_content_page_config(content_type);
    if config.uses_categories {
        let categories_route = rustelo_core_lib::routing::utils::get_localized_route(
            &format!("{}/categories", base_route),
            language,
        );
        routes.push(categories_route);
    }

    routes
}
