//! Unified admin layout 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.

use leptos::prelude::*;

// Define AdminSection here to avoid conditional compilation issues
#[derive(Clone, Debug, PartialEq)]
pub enum AdminSection {
    Dashboard,
    Users,
    Roles,
    Content,
}

#[cfg(target_arch = "wasm32")]
use super::client::{
    AdminBreadcrumb as AdminBreadcrumbClient, AdminCard as AdminCardClient,
    AdminEmptyState as AdminEmptyStateClient, AdminHeader as AdminHeaderClient,
    AdminLayout as AdminLayoutClient,
};
#[cfg(not(target_arch = "wasm32"))]
use super::ssr::{
    AdminBreadcrumb as AdminBreadcrumbSSR, AdminCard as AdminCardSSR,
    AdminEmptyState as AdminEmptyStateSSR, AdminHeader as AdminHeaderSSR,
    AdminLayout as AdminLayoutSSR,
};

impl AdminSection {
    pub fn route(&self) -> &'static str {
        match self {
            AdminSection::Dashboard => "/admin",
            AdminSection::Users => "/admin/users",
            AdminSection::Roles => "/admin/roles",
            AdminSection::Content => "/admin/content",
        }
    }

    pub fn title(&self, i18n: &::rustelo_core_lib::i18n::UnifiedI18n) -> String {
        match self {
            AdminSection::Dashboard => i18n.t("admin-dashboard-title"),
            AdminSection::Users => i18n.t("admin-users-title"),
            AdminSection::Roles => i18n.t("admin-roles-title"),
            AdminSection::Content => i18n.t("admin-content-title"),
        }
    }

    pub fn icon(&self) -> &'static str {
        match self {
            AdminSection::Dashboard => {
                "M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586l-2 2V5H5v14h7v2H4a1 1 0 01-1-1V4z"
            }
            AdminSection::Users => {
                "M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 \
                 00-9-5.197m13.5-9a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0z"
            }
            AdminSection::Roles => {
                "M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 \
                 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 \
                 0-1.042-.133-2.052-.382-3.016z"
            }
            AdminSection::Content => {
                "M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 \
                 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
            }
        }
    }
}

/// Unified admin layout component that delegates to appropriate implementation
#[component]
pub fn AdminLayout(
    current_path: ReadSignal<String>,
    #[prop(optional)] children: Option<Children>,
) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! {
            <AdminLayoutSSR
                current_path=current_path
                children=children.unwrap_or_else(|| Box::new(|| AnyView::from(Fragment::new(vec![]))))
            />
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // Client context: use reactive implementation
        view! {
            <AdminLayoutClient
                current_path=current_path
                children=children.map(|c| Box::new(move || c()) as Box<dyn FnOnce() -> AnyView + Send>).unwrap_or_else(|| Box::new(|| AnyView::from(Fragment::new(vec![]))))
            />
        }
    }
}

/// Unified admin breadcrumb component that delegates to appropriate
/// implementation
#[component]
pub fn AdminBreadcrumb(current_path: ReadSignal<String>) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! {
            <AdminBreadcrumbSSR current_path=current_path />
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // Client context: use reactive implementation
        view! {
            <AdminBreadcrumbClient current_path=current_path />
        }
    }
}

/// Unified admin header component that delegates to appropriate implementation
#[component]
pub fn AdminHeader(
    #[prop(optional)] title: Option<String>,
    #[prop(optional)] subtitle: Option<String>,
    #[prop(optional)] actions: Option<Children>,
) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! {
            <AdminHeaderSSR
                title=title.unwrap_or_default()
                subtitle=subtitle.unwrap_or_default()
                actions=actions.unwrap_or_else(|| Box::new(|| AnyView::from(Fragment::new(vec![]))))
            />
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // Client context: use reactive implementation
        view! {
            <AdminHeaderClient
                title=title.unwrap_or_default()
                subtitle=subtitle.unwrap_or_default()
                actions=actions.map(|a| Box::new(move || a()) as Box<dyn FnOnce() -> AnyView + Send>).unwrap_or_else(|| Box::new(|| AnyView::from(Fragment::new(vec![]))))
            />
        }
    }
}

/// Unified admin card component that delegates to appropriate implementation
#[component]
pub fn AdminCard(
    #[prop(optional)] title: Option<String>,
    #[prop(optional)] class: Option<String>,
    children: Children,
) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! {
            <AdminCardSSR
                title=title.unwrap_or_default()
                class=class.unwrap_or_default()
            >
                {children()}
            </AdminCardSSR>
        }
    }

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

/// Unified admin empty state component that delegates to appropriate
/// implementation
#[component]
pub fn AdminEmptyState(
    #[prop(optional)] icon: Option<String>,
    #[prop(optional)] title: Option<String>,
    #[prop(optional)] description: Option<String>,
    #[prop(optional)] action: Option<Children>,
) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! {
            <AdminEmptyStateSSR
                icon=icon.unwrap_or_default()
                title=title.unwrap_or_default()
                description=description.unwrap_or_default()
                action=action.unwrap_or_else(|| Box::new(|| AnyView::from(Fragment::new(vec![]))))
            />
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // Client context: use reactive implementation
        view! {
            <AdminEmptyStateClient
                icon=icon.unwrap_or_default()
                title=title.unwrap_or_default()
                description=description.unwrap_or_default()
                action=action.map(|a| Box::new(move || a()) as Box<dyn FnOnce() -> AnyView + Send>).unwrap_or_else(|| Box::new(|| AnyView::from(Fragment::new(vec![]))))
            />
        }
    }
}
