use crate::http::AppState;
use axum::{extract::Query, extract::State, http::StatusCode, Json};
use serde::Deserialize;
use std::sync::Arc;

#[derive(Debug, Deserialize)]
pub struct ReconcileParams {
    #[serde(default)]
    pub dry_run: bool,
}

/// POST /reconcile[?dry_run=true]
///
/// Runs one reconciliation tick against GitHub and returns the report as JSON.
/// Pass `?dry_run=true` to see what would change without applying transitions.
pub async fn handle(
    State(state): State<Arc<AppState>>,
    Query(params): Query<ReconcileParams>,
) -> Result<Json<crate::reconciliation::ReconciliationReport>, StatusCode> {
    let report = crate::reconciliation::run_once_with_runtime_config(
        &state.core.tasks,
        state.core.workflow_runtime_store.as_deref(),
        state.core.issue_workflow_store.as_deref(),
        &state.core.server.config.reconciliation,
        params.dry_run,
        state.core.server.config.server.github_token.as_deref(),
    )
    .await;

    Ok(Json(report))
}
