use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use serde_json::json;
use crate::{error::AppError, git, state::AppState, util::run_blocking, validate};
pub async fn delete_tenant(
State(state): State<AppState>,
Path(tenant_id): Path<String>,
) -> Result<impl IntoResponse, AppError> {
let tenant_id = validate::tenant_id(&tenant_id)?.to_string();
tracing::debug!(tenant_id = %tenant_id, "handling delete tenant request");
let repo_path = state.config.server.repos_path.join(&tenant_id);
let lock = state.get_repo_lock(&tenant_id);
let _lock_guard = lock.lock().await;
let tenant_id_for_task = tenant_id.clone();
run_blocking(move || git::GitTenant::delete_repo(&repo_path, &tenant_id_for_task)).await?;
state.remove_repo_lock(&tenant_id);
tracing::info!(tenant_id = %tenant_id, "tenant deleted");
Ok((StatusCode::OK, Json(json!({ "deleted": tenant_id }))))
}