use std::collections::HashSet;
use std::path::{Path, PathBuf};
use tracing::info;
use crate::error::ScanError;
use crate::port::ScanStore;
pub(super) fn reconcile_deleted_files(
project_dir: &Path,
discovered_files: &[PathBuf],
db: &dyn ScanStore,
) -> Result<(), ScanError> {
let db_paths = db.get_all_file_paths()?;
let disk_paths: HashSet<String> = discovered_files
.iter()
.filter_map(|p| {
p.strip_prefix(project_dir)
.ok()
.map(|rel| rel.to_string_lossy().to_string())
})
.collect();
for (path, file_id) in &db_paths {
if !disk_paths.contains(path) {
info!("removing stale file from index: {}", path);
db.delete_deps_for_file(*file_id)?;
db.delete_symbols_for_file(*file_id)?;
db.delete_file(*file_id)?;
}
}
Ok(())
}