use std::sync::Arc;
use axum::extract::{Path, State};
use axum::Json;
use klieo_core::ids::RunId;
use klieo_runlog::{build_worksheet, dispatched_pending_calls, ReconciliationWorksheet};
use crate::error::OpsError;
use crate::state::OpsState;
pub(crate) async fn get_reconciliation(
State(state): State<Arc<OpsState>>,
Path(run_id_str): Path<String>,
) -> Result<Json<ReconciliationWorksheet>, OpsError> {
let store = state
.run_log_store
.as_ref()
.ok_or(OpsError::StoreNotConfigured)?;
let run_id = ulid::Ulid::from_string(&run_id_str)
.map(RunId)
.map_err(|_| OpsError::RunNotFound)?;
let log = store
.get(run_id)
.await
.map_err(|e| {
tracing::error!(operation = "get_reconciliation", run_id = %run_id_str, detail = %e, "store error");
OpsError::Internal(Box::new(e))
})?
.ok_or(OpsError::RunNotFound)?;
let pending = dispatched_pending_calls(state.kv_store.as_deref(), run_id).await;
Ok(Json(build_worksheet(&log, &pending)))
}