use axum::response::{IntoResponse, Json, Response};
use utoipa::OpenApi;

use crate::{ApiDoc, AuthApiDoc, ConfigApiDoc, MessageApiDoc, WorkflowApiDoc};

/// GET /docs/openapi.json
///
/// 返回 OpenAPI 3.0 规范 JSON 文档，合并所有模块的路径和 schema。
/// 仅在 debug 模式下可用，生产构建返回 404。
pub async fn openapi_spec() -> Response {
    #[cfg(debug_assertions)]
    {
        let mut spec = ApiDoc::openapi();
        spec.merge(AuthApiDoc::openapi());
        spec.merge(ConfigApiDoc::openapi());
        spec.merge(WorkflowApiDoc::openapi());
        spec.merge(MessageApiDoc::openapi());
        Json(serde_json::to_value(spec).unwrap_or_default()).into_response()
    }

    #[cfg(not(debug_assertions))]
    {
        (axum::http::StatusCode::NOT_FOUND, "Not Found").into_response()
    }
}
