//! Generic Content Index Page - works with ANY content type dynamically
//!
//! This component is fully configuration-driven and works with any content type
//! defined in content-kinds.toml without hardcoded logic.
//!
//! Uses safe, non-reactive patterns to avoid hydration issues and panics.

use leptos::prelude::*;
use rustelo_components_leptos::content::simple_grid::SimpleContentGrid;
use rustelo_components_leptos::filter::UnifiedCategoryFilter;
use rustelo_core_lib::i18n::content_helper::create_content_provider;
use rustelo_core_lib::state::use_current_language;

/// Generic Content Index Page - works with ANY content type dynamically.
/// Title and description are rendered in reactive closures so they update
/// immediately when the user switches language without a full page reload.
#[component]
pub fn UnifiedContentIndexPage(
    content_type: String,
    _language: String,
    #[prop(optional)] lang_content: Option<std::collections::HashMap<String, String>>,
    #[prop(optional)] filter: Option<String>,
) -> impl IntoView {
    let lang_signal = use_current_language();
    let ct_h1 = content_type.clone();
    let ct_p = content_type.clone();

    view! {
        <div class="ds-container" data-content-type={content_type.clone()} data-language={_language.clone()}>
            <div class="container mx-auto px-4 py-8">
                // Page Header — reactive closures so titles update on SPA language switch
                <header class="text-center mb-12">
                    <h1 class="text-balance text-4xl font-bold tracking-tight ds-text sm:text-6xl mb-ds-4">
                        {move || {
                            let lang = {
                                #[cfg(target_arch = "wasm32")]
                                { lang_signal.get() }
                                #[cfg(not(target_arch = "wasm32"))]
                                { lang_signal.get_untracked() }
                            };
                            let translator = rustelo_core_lib::SsrTranslator::new(lang);
                            let lc = rustelo_core_lib::build_page_content_patterns(
                                &translator, &[ct_h1.as_str(), "content-"],
                            );
                            let cp = create_content_provider(Some(lc));
                            cp.t_with_prefixes("page-title", &[&ct_h1, "content"],
                                Some(&format!("{} Index", ct_h1)))
                        }}
                    </h1>
                    <p class="ds-text-secondary ds-body max-w-2xl mx-auto">
                        {move || {
                            let lang = {
                                #[cfg(target_arch = "wasm32")]
                                { lang_signal.get() }
                                #[cfg(not(target_arch = "wasm32"))]
                                { lang_signal.get_untracked() }
                            };
                            let translator = rustelo_core_lib::SsrTranslator::new(lang);
                            let lc = rustelo_core_lib::build_page_content_patterns(
                                &translator, &[ct_p.as_str(), "content-"],
                            );
                            let cp = create_content_provider(Some(lc));
                            cp.t_with_prefixes("page-description", &[&ct_p, "content"],
                                Some(&format!("Browse all {} content", ct_p)))
                        }}
                    </p>
                </header>

                // Category and Tag Filters (Unified Implementation - works in both SSR and SPA)
                <UnifiedCategoryFilter
                    content_type=content_type.clone()
                    language=_language.clone()
                    _all_emoji="🏷️".to_string()
                />

                <SimpleContentGrid
                    content_type=content_type.clone()
                    language=_language.clone()
                    lang_content=lang_content.clone().unwrap_or_default()
                    category_filter=filter.unwrap_or_default()
                />
            </div>
        </div>
    }
}
