//! Admin API routes.

use axum::{
    Router,
    routing::{delete, get, post, put},
};

use super::{auth, config, dashboard, props, realms, scenes, spots, staff, users};
use crate::app::AdminAppState;

/// Create the admin API router.
///
/// Note: HTML pages are handled by Leptos - this router only contains API endpoints.
pub fn admin_api_router() -> Router<AdminAppState> {
    Router::new()
        // API - Health
        .route("/health", get(health_check))
        // API - Dashboard
        .route("/dashboard/stats", get(dashboard::get_stats))
        // API - Auth
        .route("/auth/login", post(auth::login))
        .route("/auth/logout", post(auth::logout))
        .route("/auth/context", get(auth::get_auth_context))
        // API - Config
        .route(
            "/config",
            get(config::get_config).put(config::update_config),
        )
        // API - Staff
        .route("/staff", get(staff::list_staff).post(staff::create_staff))
        .route("/staff/{user_id}", delete(staff::delete_staff))
        // API - Users
        .route("/users", get(users::list_users).post(users::create_user))
        .route("/users/search", get(users::search_users))
        .route("/users/{user_id}", get(users::get_user))
        .route("/users/{user_id}/status", put(users::update_status))
        .route(
            "/users/{user_id}/reset-password",
            post(users::reset_password),
        )
        .route(
            "/users/{user_id}/realms",
            get(users::get_user_realms).post(users::add_to_realm),
        )
        .route(
            "/users/{user_id}/realms/{realm_id}",
            delete(users::remove_from_realm),
        )
        // API - Realms
        .route(
            "/realms",
            get(realms::list_realms).post(realms::create_realm),
        )
        .route("/realms/simple", get(users::list_realms))
        .route(
            "/realms/{slug}",
            get(realms::get_realm).put(realms::update_realm),
        )
        .route("/realms/{slug}/transfer", post(realms::transfer_ownership))
        // API - Scenes
        .route(
            "/realms/{slug}/scenes",
            get(scenes::list_scenes).post(scenes::create_scene),
        )
        .route(
            "/scenes/{scene_id}",
            get(scenes::get_scene)
                .put(scenes::update_scene)
                .delete(scenes::delete_scene),
        )
        // API - Spots
        .route(
            "/scenes/{scene_id}/spots",
            get(spots::list_spots).post(spots::create_spot),
        )
        .route(
            "/spots/{spot_id}",
            get(spots::get_spot)
                .put(spots::update_spot)
                .delete(spots::delete_spot),
        )
        // API - Props (server-wide)
        .route("/props", get(props::list_props).post(props::create_prop))
        .route(
            "/props/{prop_id}",
            get(props::get_prop).delete(props::delete_prop),
        )
}

/// Health check endpoint.
async fn health_check() -> &'static str {
    "Admin API OK"
}
