//! Realms list page component.

use leptos::prelude::*;

use crate::components::{
    Card, EmptyState, NsfwBadge, PageHeader, Pagination, PrivacyBadge, SearchForm,
};
use crate::hooks::{use_fetch, use_pagination};
use crate::models::RealmSummary;
use crate::utils::build_paginated_url;

/// Realms page component.
#[component]
pub fn RealmsPage() -> impl IntoView {
    let pagination = use_pagination();

    let realms = use_fetch::<Vec<RealmSummary>>(move || {
        build_paginated_url(
            "/api/admin/realms",
            pagination.page.get(),
            &pagination.search_query.get(),
            25,
        )
    });

    view! {
        <PageHeader title="All Realms" subtitle="Manage realm spaces">
            <a href="/admin/realms/new" class="btn btn-primary">"Create Realm"</a>
        </PageHeader>

        <Card>
            <SearchForm
                action="/admin/realms"
                placeholder="Search by name or slug..."
                search_input=pagination.search_input
            />

            <Suspense fallback=|| view! { <p>"Loading realms..."</p> }>
                {move || {
                    realms.get().map(|maybe_realms: Option<Vec<RealmSummary>>| {
                        match maybe_realms {
                            Some(realm_list) if !realm_list.is_empty() => {
                                view! {
                                    <div class="table-container">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>"Name"</th>
                                                    <th>"Tagline"</th>
                                                    <th>"Privacy"</th>
                                                    <th>"NSFW"</th>
                                                    <th>"Owner"</th>
                                                    <th>"Members"</th>
                                                    <th>"Online"</th>
                                                    <th>"Created"</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {realm_list.into_iter().map(|realm| {
                                                    view! {
                                                        <tr>
                                                            <td>
                                                                <a href=format!("/admin/realms/{}", realm.slug) class="table-link">
                                                                    {realm.name}
                                                                </a>
                                                            </td>
                                                            <td>{realm.tagline.unwrap_or_default()}</td>
                                                            <td><PrivacyBadge privacy=realm.privacy /></td>
                                                            <td>
                                                                {if realm.is_nsfw {
                                                                    view! { <NsfwBadge /> }.into_any()
                                                                } else {
                                                                    view! { <span>"-"</span> }.into_any()
                                                                }}
                                                            </td>
                                                            <td>
                                                                <a href=format!("/admin/users/{}", realm.owner_id) class="table-link">
                                                                    {realm.owner_username}
                                                                </a>
                                                            </td>
                                                            <td>{realm.member_count}</td>
                                                            <td>{realm.current_user_count}</td>
                                                            <td>{realm.created_at}</td>
                                                        </tr>
                                                    }
                                                }).collect_view()}
                                            </tbody>
                                        </table>
                                    </div>

                                    <Pagination
                                        current_page=pagination.page.get()
                                        base_url="/admin/realms".to_string()
                                        query=pagination.search_query.get()
                                    />
                                }.into_any()
                            }
                            _ => view! {
                                <EmptyState
                                    message="No realms found."
                                    action_href="/admin/realms/new"
                                    action_text="Create Realm"
                                />
                            }.into_any()
                        }
                    })
                }}
            </Suspense>
        </Card>
    }
}
