//! Dashboard page component.

use leptos::prelude::*;

use crate::components::{Card, PageHeader};
use crate::hooks::use_fetch;
use crate::models::DashboardStats;

/// Dashboard page component.
#[component]
pub fn DashboardPage() -> impl IntoView {
    let stats = use_fetch::<DashboardStats>(|| "/api/admin/dashboard/stats".to_string());

    view! {
        <PageHeader
            title="Dashboard"
            subtitle="Server overview and quick stats"
        />

        <div class="dashboard-grid">
            <Suspense fallback=|| view! { <p>"Loading stats..."</p> }>
                {move || {
                    stats.get().map(|maybe_stats| {
                        match maybe_stats {
                            Some(s) => view! {
                                <StatCard title="Total Users" value=s.total_users.to_string() />
                                <StatCard title="Active Users" value=s.active_users.to_string() />
                                <StatCard title="Total Realms" value=s.total_realms.to_string() />
                                <StatCard title="Online Now" value=s.online_users.to_string() />
                                <StatCard title="Staff Members" value=s.staff_count.to_string() />
                            }.into_any(),
                            None => view! {
                                <StatCard title="Total Users" value="-".to_string() />
                                <StatCard title="Active Users" value="-".to_string() />
                                <StatCard title="Total Realms" value="-".to_string() />
                                <StatCard title="Online Now" value="-".to_string() />
                                <StatCard title="Staff Members" value="-".to_string() />
                            }.into_any()
                        }
                    })
                }}
            </Suspense>
        </div>

        <div class="dashboard-sections">
            <Card title="Quick Actions">
                <div class="quick-actions">
                    <a href="/admin/users/new" class="btn btn-primary">"Create User"</a>
                    <a href="/admin/realms/new" class="btn btn-primary">"Create Realm"</a>
                    <a href="/admin/staff" class="btn btn-secondary">"Manage Staff"</a>
                    <a href="/admin/config" class="btn btn-secondary">"Server Config"</a>
                </div>
            </Card>

            <Card title="Recent Activity">
                <p class="text-muted">"Activity feed coming soon..."</p>
            </Card>
        </div>
    }
}

/// Stat card component.
#[component]
fn StatCard(title: &'static str, value: String) -> impl IntoView {
    view! {
        <div class="stat-card">
            <div class="stat-value">{value}</div>
            <div class="stat-title">{title}</div>
        </div>
    }
}
