//! 路由注册 — 聚合所有子路由
//!
//! 认证中间件保护所有 /api/* 路由（除 /api/auth/login 和 /api/health 外）。
//! 权限校验在各路由处理器内部通过 `check_permission` 完成。

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

use crate::middleware::auth::auth_middleware;
use crate::tcp::commands::{self, AppState};

mod auth;
mod cabinets;
mod cabinet_tree;
mod charge_records;
mod device_logs;
mod downloads;
mod energy_stats;
mod h5;
mod health;
mod operation_logs;
mod organizations;
mod projects;
mod roles;
mod users;
mod users_perm;

/// 构建完整路由树（需要传入 AppState 用于认证中间件）
pub fn build(state: AppState) -> Router {
    let protected = Router::<AppState>::new()
        // 认证相关
        .route("/api/auth/logout", post(auth::logout))
        .route("/api/auth/me", get(auth::me))
        // 设备指令下发
        .route("/api/devices/:dev_id/command", post(commands::send_command))
        .route("/api/devices/stats", get(commands::device_stats))
        // 角色管理
        .route("/api/roles", get(roles::list_roles).post(roles::create_role))
        .route("/api/roles/:id", put(roles::update_role).delete(roles::delete_role))
        .route("/api/roles/:id/permissions", put(roles::set_role_permissions))
        // 权限码列表
        .route("/api/permissions", get(roles::list_permissions))
        // 用户权限管理
        .route("/api/users/:id/permissions", get(users_perm::get_user_permissions))
        .route("/api/users/:id/roles", put(users_perm::set_user_roles))
        .route("/api/users/:id/scopes", put(users_perm::set_user_scopes).get(users_perm::get_user_scopes))
        // 组织管理
        .route("/api/organizations", get(organizations::list_organizations).post(organizations::create_organization))
        .route("/api/organizations/:id", put(organizations::update_organization).delete(organizations::delete_organization))
        // 项目管理
        .route("/api/organizations/:org_id/projects", get(projects::list_projects).post(projects::create_project))
        .route("/api/projects/:id", put(projects::update_project).delete(projects::delete_project))
        // 设备管理
        .route("/api/projects/:project_id/cabinets", get(cabinets::list_cabinets))
        .route("/api/cabinets", post(cabinets::create_cabinet))
        .route("/api/cabinets/:id", put(cabinets::update_cabinet).delete(cabinets::delete_cabinet).get(cabinets::get_cabinet_detail))
        .route("/api/cabinets/:id/regenerate-auth", post(cabinets::regenerate_auth))
        // 组织树
        .route("/api/organization-tree", get(cabinet_tree::organization_tree))
        // 充电记录
        .route("/api/charge-records", get(charge_records::list_charge_records))
        .route("/api/charge-records/export", get(charge_records::export_charge_records))
        // 设备日志
        .route("/api/device-logs", get(device_logs::list_device_logs))
        // 下载中心
        .route("/api/downloads", get(downloads::list_downloads))
        .route("/api/downloads/:id", get(downloads::get_download))
        .route("/api/downloads/:id/file", get(downloads::download_file))
        .route("/api/downloads/cleanup", post(downloads::cleanup_downloads))
        // 设备级联下拉
        .route("/api/cabinets/options", get(cabinets::list_cabinet_options))
        .route("/api/cabin-boards/options", get(cabinets::list_cabin_board_options))
        .route("/api/compartments/options", get(cabinets::list_compartment_options))
        // 能耗管理
        .route("/api/energy-stats/summary", get(energy_stats::get_summary))
        .route("/api/energy-stats/cabinets", get(energy_stats::get_cabinet_ranking))
        .route("/api/energy-stats/projects", get(energy_stats::get_project_stats))
        .route("/api/energy-stats/trend", get(energy_stats::get_trend))
        .route("/api/energy-stats/export", get(energy_stats::export_energy_stats))
        // 用户管理
        .route("/api/users", get(users::list_users).post(users::create_user))
        .route("/api/users/:id", put(users::update_user).delete(users::delete_user))
        .route("/api/users/:id/reset-password", put(users::reset_password))
        .route("/api/users/:id/status", put(users::set_user_status))
        // 操作日志
        .route("/api/operation-logs", get(operation_logs::list_operation_logs))
        // H5 用户端接口（需认证）
        .route("/api/h5/dashboard", get(h5::get_dashboard))
        .route("/api/h5/projects", get(h5::get_projects))
        .route("/api/h5/cabinets", get(h5::get_cabinets))
        .route("/api/h5/cabinets/abstract/:abstract_id", get(h5::get_cabinet_by_abstract_id))
        .route("/api/h5/cabinets/:id", get(h5::get_cabinet_detail))
        .route("/api/h5/compartments/:id", get(h5::get_compartment_detail))
        .route("/api/h5/charge/start", post(h5::start_charge))
        .route("/api/h5/charge/stop", post(h5::stop_charge))
        .route("/api/h5/door/open", post(h5::open_door))
        // 认证中间件层
        .layer(middleware::from_fn_with_state(state.clone(), auth_middleware));

    Router::<AppState>::new()
        .route("/api/health", get(health::health_check))
        .route("/api/auth/login", post(auth::login))
        .route("/api/h5/auth/login", post(h5::h5_login))
        .merge(protected)
        .with_state(state)
}
