//! Unified theme system component interface using shared delegation patterns
//!
//! This module provides a unified interface that automatically selects between
//! client-side reactive and server-side static implementations based on
//! context.

// Re-export shared theme types for easy access
pub use ::rustelo_core_lib::{EffectiveTheme, ThemeMode, ThemeUtils};
use leptos::prelude::*;

#[cfg(target_arch = "wasm32")]
use crate::theme::client::{
    use_theme as use_theme_client, DarkModeToggle as DarkModeToggleClient, ThemeContext,
    ThemeProvider as ClientThemeProvider,
};
#[cfg(not(target_arch = "wasm32"))]
use crate::theme::ssr::{DarkModeToggleSSR, ThemeProviderSSR};

/// Unified theme provider component that delegates to appropriate
/// implementation
#[component]
pub fn ThemeProvider(
    #[prop(optional)] default_theme: Option<ThemeMode>,
    #[prop(optional)] language: Option<String>,
    children: leptos::children::ChildrenFn,
) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! {
            <ThemeProviderSSR default_theme=default_theme.unwrap_or_default() language=language.unwrap_or_default()>
                {children()}
            </ThemeProviderSSR>
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // Client context: use reactive implementation
        // Note: Pass optional props directly - both unified and client have same
        // signature
        view! {
            <ClientThemeProvider
                _default_theme=default_theme.unwrap_or(rustelo_core_lib::ThemeMode::System)
                _language=language.unwrap_or_default()
            >
                {children()}
            </ClientThemeProvider>
        }
    }
}

/// Unified dark mode toggle component that delegates to appropriate
/// implementation
#[component]
pub fn DarkModeToggle(#[prop(optional)] class: Option<String>) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! { <DarkModeToggleSSR class=class.unwrap_or_default() /> }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // Client context: use reactive implementation
        view! { <DarkModeToggleClient class=class.unwrap_or_default() /> }
    }
}

/// Unified theme hook (client-only)
#[cfg(target_arch = "wasm32")]
pub fn use_theme() -> ThemeContext {
    use_theme_client()
}
