pub mod auth;
pub mod backlog_ncl;
pub mod drift_watcher;
pub mod handlers;
pub mod login;
pub mod ncl_write;
pub mod qa_ncl;
pub mod search_bookmarks_ncl;
pub mod watcher;

pub use drift_watcher::DriftWatcher;
pub use watcher::TemplateWatcher;

use crate::api::AppState;

pub fn router(state: AppState) -> axum::Router {
    multi_router(state)
}

#[allow(dead_code)]
fn single_router(state: AppState) -> axum::Router {
    use axum::routing::{get, post};
    axum::Router::new()
        .route("/", get(handlers::dashboard))
        .route("/graph", get(handlers::graph))
        .route("/sessions", get(handlers::sessions))
        .route("/notifications", get(handlers::notifications_page))
        .route("/modes", get(handlers::modes))
        .route("/search", get(handlers::search_page))
        .route("/assets/{*path}", get(handlers::serve_asset_single))
        .route("/public/{*path}", get(handlers::serve_public_single))
        .route("/backlog", get(handlers::backlog_page))
        .route("/backlog/status", post(handlers::backlog_update_status))
        .route("/backlog/add", post(handlers::backlog_add))
        .route("/manage", get(handlers::manage_page))
        .route("/manage/add", post(handlers::manage_add))
        .route("/manage/remove", post(handlers::manage_remove))
        .route("/actions", get(handlers::actions_page))
        .route("/actions/run", post(handlers::actions_run))
        .route("/qa", get(handlers::qa_page))
        .route("/qa/delete", post(handlers::qa_delete))
        .route("/qa/update", post(handlers::qa_update))
        .route("/search/bookmark/add", post(handlers::search_bookmark_add))
        .route(
            "/search/bookmark/delete",
            post(handlers::search_bookmark_delete),
        )
        .with_state(state)
}

fn multi_router(state: AppState) -> axum::Router {
    use axum::routing::{get, post};
    axum::Router::new()
        // Project picker and management at root
        .route("/", get(handlers::project_picker))
        .route("/manage", get(handlers::manage_page_guarded))
        .route("/manage/add", post(handlers::manage_add_guarded))
        .route("/manage/remove", post(handlers::manage_remove_guarded))
        .route(
            "/manage/login",
            get(login::manage_login_page).post(login::manage_login_submit),
        )
        .route("/manage/logout", get(handlers::manage_logout))
        // Per-project routes — AuthUser extractor enforces auth per project
        .route("/{slug}/", get(handlers::dashboard_mp))
        .route("/{slug}/stats", get(handlers::dashboard_stats_mp))
        .route("/{slug}/graph", get(handlers::graph_mp))
        .route("/{slug}/sessions", get(handlers::sessions_mp))
        .route("/{slug}/notifications", get(handlers::notifications_mp))
        .route("/{slug}/modes", get(handlers::modes_mp))
        .route("/{slug}/logout", get(login::logout))
        .route("/{slug}/search", get(handlers::search_page_mp))
        .route("/{slug}/assets/{*path}", get(handlers::serve_asset_mp))
        .route("/{slug}/public/{*path}", get(handlers::serve_public_mp))
        .route("/{slug}/backlog", get(handlers::backlog_page_mp))
        .route(
            "/{slug}/backlog/status",
            post(handlers::backlog_update_status_mp),
        )
        .route("/{slug}/backlog/add", post(handlers::backlog_add_mp))
        .route("/{slug}/backlog/edit", post(handlers::backlog_edit_mp))
        .route("/{slug}/backlog/delete", post(handlers::backlog_delete_mp))
        .route(
            "/{slug}/backlog/propose-status",
            post(handlers::backlog_propose_status_mp),
        )
        .route(
            "/{slug}/notifications/{id}/action",
            post(handlers::notification_action_mp),
        )
        .route(
            "/{slug}/notifications/emit",
            post(handlers::emit_notification_mp),
        )
        .route("/{slug}/compose", get(handlers::compose_page_mp))
        .route(
            "/{slug}/compose/form/{form_id}",
            get(handlers::compose_form_schema_mp),
        )
        .route("/{slug}/compose/send", post(handlers::compose_send_mp))
        .route("/{slug}/api", get(handlers::api_catalog_page_mp))
        .route("/{slug}/config", get(handlers::config_page_mp))
        .route("/{slug}/adrs", get(handlers::adrs_page_mp))
        .route("/{slug}/personal", get(handlers::personal_page_mp))
        .route("/{slug}/career", get(handlers::career_page_mp))
        .route("/{slug}/provisioning", get(handlers::provisioning_page_mp))
        .route(
            "/{slug}/migrations/pending",
            get(handlers::pending_migrations_fragment_mp),
        )
        .route("/{slug}/actions", get(handlers::actions_page_mp))
        .route("/{slug}/actions/run", post(handlers::actions_run_mp))
        .route("/{slug}/qa", get(handlers::qa_page_mp))
        .route("/{slug}/qa/delete", post(handlers::qa_delete))
        .route("/{slug}/qa/update", post(handlers::qa_update))
        .route(
            "/{slug}/search/bookmark/add",
            post(handlers::search_bookmark_add),
        )
        .route(
            "/{slug}/search/bookmark/delete",
            post(handlers::search_bookmark_delete),
        )
        // Login is public — no AuthUser extractor
        .route(
            "/{slug}/login",
            get(login::login_page).post(login::login_submit),
        )
        .with_state(state)
}
