use axum::Json;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};

/// 冻结模块路径前缀列表。
///
/// 这些模块前端已通过 FROZEN_ROUTES 守卫拦截，后端也需同步拦截，
/// 防止直接调 API 绕过限制。
const FROZEN_PREFIXES: &[&str] = &[
    "/api/v1/health/care-plans",
    "/api/v1/health/shifts",
    "/api/v1/health/family-proxy",
    "/api/v1/health/medications",
    "/api/v1/health/dialysis",
    "/api/v1/health/schedules",
];

pub async fn frozen_module_middleware(req: Request<Body>, next: Next) -> Response {
    let path = req.uri().path();

    for prefix in FROZEN_PREFIXES {
        if path.starts_with(prefix) {
            return (
                StatusCode::FORBIDDEN,
                Json(serde_json::json!({
                    "success": false,
                    "error": "该功能正在优化中，暂不可用"
                })),
            )
                .into_response();
        }
    }

    next.run(req).await
}
