//! SSR Unified Content Category Page Component
//! Server-side rendered content category page that works with any content type

use leptos::prelude::*;
use rustelo_components_leptos::content::UnifiedContentGrid;
use rustelo_core_lib::i18n::{build_page_content_patterns, SsrTranslator};

/// SSR Unified Content Category Page - works with any content type category
/// Uses pattern-based key discovery for automatic i18n support
#[component]
pub fn ContentCategoryPageSSR(
    content_type: String,
    category: String,
    _language: String,
) -> impl IntoView {
    // Use provided content type directly
    let resolved_content_type = content_type;

    let resolved_category = category;

    // Use pattern-based key discovery for the content type
    let ssr_i18n = SsrTranslator::new(_language.clone());
    let pattern_str = format!("{}-", resolved_content_type);
    let patterns = [pattern_str.as_str(), "content-", "nav-"];
    let content = build_page_content_patterns(&ssr_i18n, &patterns);

    // Clean closure pattern that works with Leptos ownership model
    let content_map = Some(content);
    let get_content = move |key: &str| -> String {
        if let Some(ref simple) = content_map {
            simple.get(key).cloned().unwrap_or_else(|| {
                tracing::warn!("i18n key not found: '{}'", key);
                String::new()
            })
        } else {
            tracing::warn!("i18n key not found (no content): '{}'", key);
            String::new()
        }
    };

    // Generate category-specific keys first, fallback to generic keys
    let category_title_key = format!("{}-cat-{}-title", resolved_content_type, resolved_category);
    let category_desc_key = format!("{}-cat-{}-desc", resolved_content_type, resolved_category);
    let page_title_key = format!("{}-category-page-title", resolved_content_type);
    let page_description_key = format!("{}-category-page-description", resolved_content_type);

    // Get category-specific title and description with fallbacks
    let category_title = get_content(&category_title_key);
    let category_description = get_content(&category_desc_key);

    // Get category display name for fallback
    let category_display =
        rustelo_core_lib::content_resolver::get_content_type_display_name(&resolved_category);

    // Use category-specific content if available, otherwise fallback to generic +
    // category name
    let display_title = if !category_title.is_empty() {
        category_title
    } else {
        // Fallback to generic page title + category name
        format!("{} - {}", get_content(&page_title_key), category_display)
    };

    let display_description = if !category_description.is_empty() {
        category_description
    } else {
        // Fallback to generic page description
        get_content(&page_description_key)
    };

    view! {
        <div class="ds-bg-page">
            // Hero section with category
            <section class="py-ds-4 ds-container ds-rounded-lg ds-shadow-lg">
                <div class="mx-auto max-w-4xl text-center">
                    <h1 class="text-balance text-4xl font-bold tracking-tight ds-text sm:text-6xl mb-ds-4">
                        {display_title}
                    </h1>
                    <p class="ds-text-secondary ds-body max-w-2xl mx-auto">
                        {display_description}
                    </p>
                </div>
            </section>

            // Filtered content grid
            <section class="py-ds-6">
                <div class="ds-container">
                    <UnifiedContentGrid
                        content_type=resolved_content_type.clone()
                        language=_language.clone()
                        show_categories=true
                        category_filter=resolved_category
                        limit=12
                    />
                </div>
            </section>
        </div>
    }
}
