//! Server-side navigation menu component
//!
//! Static implementation of the navigation menu optimized for SSR rendering.

use leptos::prelude::*;
// Fix Leptos macro conflicts with our 'core' crate
use std::{default::Default, marker::PhantomData};
use ::rustelo_core_lib::{
    config::LogoConfig, i18n::build_page_content_patterns,
    load_reactive_menu_for_language, SsrTranslator,
};

// Import other SSR components that this depends on
// use crate::logo::LogoLink;
use crate::navigation::LanguageSelector;
use crate::theme::DarkModeToggle;

#[component]
pub fn NavMenuSSR(#[prop(optional)] language: Option<String>) -> impl IntoView {
    let lang = language.unwrap_or_else(|| rustelo_core_lib::config::get_default_language().to_string());

    // Use pattern-based key discovery - same as client for perfect hydration sync
    let ssr_i18n = SsrTranslator::new(lang.clone());
    let nav_content = build_page_content_patterns(&ssr_i18n, &["nav-"]);

    // Get logo configuration from environment variables (SSR safe)
    let logo_config = LogoConfig::from_env();

    // Load menu items for the current language
    let menu_items = match load_reactive_menu_for_language(&lang) {
        Ok(items) => items,
        Err(_) => vec![], // Fallback to empty menu on error
    };

    view! {
        <nav class="border-b border-gray-200 shadow-sm">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="flex justify-between h-16">
                    <div class="flex items-center">
                        <a href="/" class="flex items-center space-x-2">
                            <img src="/logos/jesusperez-logo-b.png" alt="Logo" class="h-8 w-auto" />
                        </a>
                    </div>
                    <div class="flex items-center space-x-4">
                        {menu_items.into_iter().map(|item| {
                            let label = item.label.translations.get(&lang).unwrap_or(&item.route).clone();
                            view! {
                                <a href={item.route.clone()} class="text-gray-700 hover:text-gray-900 px-3 py-2 text-sm font-medium">
                                    {label}
                                </a>
                            }
                        }).collect::<Vec<_>>()}
                        <div class="flex items-center space-x-2">
                            <DarkModeToggle class="btn btn-ghost btn-sm".to_string() />
                            <LanguageSelector
                                current_language=lang.clone()
                                class="".to_string()
                                navigation_signals=None
                            />
                        </div>
                    </div>
                </div>
            </div>
        </nav>
    }
}
