//! Admin API endpoints — protected by `x-admin-api-key` header.
//!
//! These endpoints are called by the Admin Dashboard (Next.js) to perform
//! platform-wide operations that bypass normal user-session authorization.

use actix_web::web;

pub mod alerts;
pub mod ai_models;
pub mod billing;
pub mod sync;

pub fn init_admin_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/api/admin")
            .route("/health", web::get().to(health_check))
            .route("/ai/sync", web::post().to(sync::admin_sync_models))
            .route("/alerts/check", web::post().to(alerts::admin_check_alerts))
            .route(
                "/workspaces/{slug}/add-credit",
                web::post().to(billing::admin_workspace_add_credit),
            )
            // Provider CRUD
            .route("/ai/providers", web::post().to(ai_models::admin_provider_create))
            .route("/ai/providers/{id}", web::patch().to(ai_models::admin_provider_update))
            .route("/ai/providers/{id}", web::delete().to(ai_models::admin_provider_delete))
            // Model CRUD
            .route("/ai/models", web::post().to(ai_models::admin_model_create))
            .route("/ai/models/{id}", web::patch().to(ai_models::admin_model_update))
            .route("/ai/models/{id}", web::delete().to(ai_models::admin_model_delete))
            // Version CRUD
            .route("/ai/versions", web::post().to(ai_models::admin_version_create))
            .route("/ai/versions/{id}", web::patch().to(ai_models::admin_version_update))
            .route("/ai/versions/{id}", web::delete().to(ai_models::admin_version_delete))
            // Pricing update
            .route("/ai/pricing/{id}", web::patch().to(ai_models::admin_pricing_update)),
    );
}

#[utoipa::path(
    get,
    path = "/api/admin/health",
    responses(
        (status = 200, description = "Admin API is healthy"),
    ),
    tag = "Admin"
)]
async fn health_check() -> impl actix_web::Responder {
    actix_web::HttpResponse::Ok().json(serde_json::json!({ "ok": true }))
}
