//! Client-side navigation menu wrapper with context-based menu loading
//!
//! This component loads menu items from the MenuRegistry context (SSR-pre-loaded)
//! and provides them to the unified menu component for instant language switching.

use ::rustelo_core_lib::state::use_language;
use leptos::prelude::*;

use crate::navigation::MenuRegistry;

/// Client-side navigation menu wrapper with context-based loading
#[component]
pub fn NavMenuClient(set_path: WriteSignal<String>, path: ReadSignal<String>) -> impl IntoView {
    // Get reactive language context
    let language_context = use_language();

    // Get menu registry from context (SSR-pre-loaded)
    let menu_registry = use_context::<MenuRegistry>().unwrap_or_else(|| {
        tracing::warn!("🔧 NavMenuClient: MenuRegistry context not found, using empty registry");
        MenuRegistry::new()
    });

    // Render function that reactively responds to language changes
    move || {
        let current_lang = language_context.current.with(|lang| lang.clone());

        // Get menu items for current language from registry
        let menu_items = menu_registry.get_cloned(&current_lang).unwrap_or_default();

        tracing::info!(
            "🔧 NavMenuClient: Rendering menu for language '{}' ({} items)",
            current_lang,
            menu_items.len()
        );

        // Note: i18n content is now loaded automatically by UnifiedNavMenu from FTL registry
        // if not provided as a prop, so we don't need to extract it here
        view! {
            <crate::navigation::navmenu::unified::UnifiedNavMenu
                navigation_signals=(path, set_path)
                language=current_lang
                menu_items=menu_items
            />
        }
    }
}

// Re-export as NavMenu for compatibility
pub use NavMenuClient as NavMenu;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_menu_registry_empty() {
        let registry = MenuRegistry::new();
        assert_eq!(registry.language_count(), 0);
        assert!(registry.available_languages().is_empty());
    }

    #[test]
    fn test_menu_registry_insert_and_get() {
        let mut registry = MenuRegistry::new();
        let menu_items = vec![];

        registry.insert("en", menu_items.clone());

        assert_eq!(registry.language_count(), 1);
        assert!(registry.contains_language("en"));
        assert_eq!(registry.get("en"), Some(&menu_items));
    }

    #[test]
    fn test_menu_registry_multiple_languages() {
        let mut registry = MenuRegistry::new();
        registry.insert("en", vec![]);
        registry.insert("es", vec![]);
        registry.insert("fr", vec![]);

        assert_eq!(registry.language_count(), 3);

        let languages = registry.available_languages();
        assert!(languages.contains(&"en".to_string()));
        assert!(languages.contains(&"es".to_string()));
        assert!(languages.contains(&"fr".to_string()));
    }

    #[test]
    fn test_menu_registry_get_cloned() {
        let mut registry = MenuRegistry::new();
        registry.insert("en", vec![]);

        let cloned = registry.get_cloned("en");
        assert!(cloned.is_some());
        assert_eq!(cloned.unwrap(), vec![]);
    }

    #[test]
    fn test_menu_registry_get_missing_language() {
        let registry = MenuRegistry::new();
        assert!(registry.get("en").is_none());
        assert!(registry.get_cloned("en").is_none());
        assert!(!registry.contains_language("en"));
    }
}
