use std::path::Path;
#[must_use]
pub fn handle(action: &str, project_root: &Path) -> String {
match action {
"status" => {
crate::core::index_orchestrator::status_json(project_root.to_string_lossy().as_ref())
}
"build" => {
crate::core::index_orchestrator::ensure_all_background(
project_root.to_string_lossy().as_ref(),
);
"started".to_string()
}
"build-full" => {
let bm25 = crate::core::bm25_index::BM25Index::index_file_path(project_root);
let _ = std::fs::remove_file(&bm25);
crate::core::graph_index::purge_index(project_root.to_string_lossy().as_ref());
let vectors_dir = crate::core::index_namespace::vectors_dir(project_root);
let _ = std::fs::remove_file(vectors_dir.join("embeddings.bin"));
let _ = std::fs::remove_file(vectors_dir.join("embeddings.json"));
crate::core::index_orchestrator::ensure_all_background(
project_root.to_string_lossy().as_ref(),
);
crate::core::index_orchestrator::build_semantic(
project_root.to_string_lossy().as_ref(),
);
crate::core::graph_cache::invalidate(Some(project_root.to_string_lossy().as_ref()));
"started".to_string()
}
"build-semantic" => {
let root = project_root.to_string_lossy();
let disk = crate::core::index_orchestrator::disk_status(&root);
if !disk.bm25_index.exists {
crate::core::index_orchestrator::ensure_all_background(&root);
}
crate::core::index_orchestrator::build_semantic(&root);
let sem = crate::core::index_orchestrator::semantic_summary(&root);
match sem.state {
"ready" => "semantic index ready".to_string(),
"failed" => format!(
"semantic index failed: {}",
sem.last_error.unwrap_or_else(|| "unknown".to_string())
),
_ => sem
.note
.unwrap_or("semantic index not available".to_string()),
}
}
_ => "Unknown action. Use: status, build, build-full, build-semantic".to_string(),
}
}