//! API routes for user UI.
//!
//! This router provides READ-ONLY access to realms, scenes, and spots.
//! All create/update/delete operations are handled by the admin-ui.
//! Channel presence is handled via WebSocket.

use axum::{routing::get, Router};

use super::{auth, avatars, inventory, realms, scenes, websocket};
use crate::app::AppState;

/// Build the API router for user UI.
///
/// Note: This router is READ-ONLY for realms/scenes/spots.
/// Auth routes (login, logout, signup, join-realm) are allowed.
/// Channel presence (join, leave, position, emotion, members) is handled via WebSocket.
pub fn api_router() -> Router<AppState> {
    Router::new()
        // Auth routes (these are user-facing operations)
        .route("/auth/me", get(auth::get_current_user))
        .route("/auth/login", axum::routing::post(auth::login))
        .route("/auth/logout", axum::routing::post(auth::logout))
        .route("/auth/signup", axum::routing::post(auth::signup))
        .route("/auth/guest", axum::routing::post(auth::guest_login))
        .route("/auth/join-realm", axum::routing::post(auth::join_realm))
        .route(
            "/auth/reset-password",
            axum::routing::post(auth::reset_password),
        )
        // Realm routes (READ-ONLY)
        .route("/realms", get(realms::list_realms))
        .route("/realms/{slug}", get(realms::get_realm))
        .route("/realms/{slug}/available", get(realms::check_slug_available))
        // Scene routes (READ-ONLY)
        .route("/realms/{slug}/entry-scene", get(scenes::get_entry_scene))
        .route("/realms/{slug}/scenes", get(scenes::list_scenes))
        .route("/realms/{slug}/scenes/{scene_slug}", get(scenes::get_scene))
        // Spot routes (READ-ONLY)
        .route(
            "/realms/{slug}/scenes/{scene_slug}/spots",
            get(scenes::list_spots),
        )
        .route(
            "/realms/{slug}/scenes/{scene_slug}/spots/{spot_id}",
            get(scenes::get_spot),
        )
        // WebSocket route for channel presence (handles join, leave, position, emotion, members)
        .route(
            "/realms/{slug}/channels/{channel_id}/ws",
            get(websocket::ws_handler::<AppState>),
        )
        // Avatar routes (require authentication)
        .route("/realms/{slug}/avatar", get(avatars::get_avatar))
        // Inventory routes (require authentication)
        .route("/inventory", get(inventory::get_inventory))
        .route(
            "/inventory/{item_id}",
            axum::routing::delete(inventory::drop_item),
        )
}
