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

use theseus_core::ApiResponse;

use crate::AppState;

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

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