use axum::{
    extract::{Path, State},
    http::StatusCode,
    routing::get,
    Json, Router,
};

use crate::types::{ApiResponse, CheckResult};

use crate::AppState;

pub(crate) fn router() -> Router<AppState> {
    Router::new().route("/api/v1/projects/:project_id/check", get(get_check))
}

async fn get_check(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
) -> Result<Json<ApiResponse<CheckResult>>, StatusCode> {
    let pid = crate::resolve_project_id(&state.db, &project_id).await?;
    let result = state
        .db
        .check(pid)
        .map_err(|err| crate::db_error_status("check.graph", "project", err))?;
    Ok(Json(ApiResponse::ok(result)))
}
