mod bridge;
mod command_support;
mod compat;
mod documents;
mod editor;
mod health;
mod hermes;
mod kernel;
mod query_support;
mod snapshot_support;
mod sse;
mod stream_support;
mod tree;
mod ws;

use crate::app::AppState;
use axum::routing::{get, post};
use axum::Router;

pub fn build_router(state: AppState) -> Router {
    let hermes_base_path = state.config().hermes_base_path.clone();
    let compat_next_base_path = state.config().compat_next_base_path.clone();
    let enable_debug_shell_routes = state.config().enable_debug_shell_routes;

    let mut router = Router::new()
        .route("/health", get(health::health))
        .route("/api/documents/meta", get(documents::meta))
        .route("/api/documents/content", get(documents::content))
        .route("/api/documents/save", post(documents::save))
        .route(
            "/api/documents/runtime/transform",
            post(editor::transform_runtime_snapshot),
        )
        .route(
            "/api/tree/projections/sidebar",
            get(kernel::project_tree_sidebar),
        )
        .route("/api/tree/projections/page", get(kernel::project_tree_page))
        .route("/api/tree/projections/file", get(kernel::project_tree_file))
        .route(
            "/api/kernel/projections/sidebar",
            get(kernel::project_sidebar),
        )
        .route("/api/kernel/subtree", get(kernel::subtree))
        .route("/api/kernel/edges", get(kernel::edges))
        .route("/api/kernel/graph", get(kernel::graph))
        .route("/api/tree/commands", post(tree::tree_command))
        .route("/api/bridge/workspace", get(bridge::workspace))
        .route("/api/bridge/request", get(bridge::request))
        .route("/api/bridge/trace", get(bridge::trace))
        .route("/api/stream/events", get(sse::events))
        .route("/api/realtime/ws", get(ws::socket))
        .nest(
            &hermes_base_path,
            Router::new()
                .route("/health", get(hermes::health))
                .route("/bridge", post(hermes::bridge_runtime)),
        )
        .nest(
            &compat_next_base_path,
            Router::new()
                .route("/ai-agent/run", post(compat::next_ai_agent_run))
                .route("/sidebar", get(compat::next_sidebar)),
        );

    if enable_debug_shell_routes {
        router = router
            .route("/tree", get(tree::tree_shell))
            .route("/document-debug", get(editor::document_editor_shell))
            .route("/document", get(editor::document_editor_shell));
    }

    router.with_state(state)
}
