use anyhow::Result;
use std::path::Path;
use std::process::Command;
use crate::utils::terminal;
use crate::core::config::schema::MorphCliSchema;
use crate::core::detection::workspace::detect_workspaces;
pub fn execute(path: &Path) -> Result<()> {
terminal::print_info("Running repository health checks...");
println!();
let mut has_errors = false;
print!("Checking config parsing... ");
let config_path = path.join("morph-cli.toml");
if config_path.exists() {
if let Ok(content) = std::fs::read_to_string(&config_path) {
match toml::from_str::<MorphCliSchema>(&content) {
Ok(_) => println!("{}", terminal::success_prefix()),
Err(e) => {
println!("{}", terminal::error_prefix());
println!(" Invalid morph-cli.toml: {}", e);
has_errors = true;
}
}
} else {
println!("{}", terminal::error_prefix());
println!(" morph-cli.toml exists but cannot be read.");
has_errors = true;
}
} else {
println!("{} (not found, using defaults)", terminal::success_prefix());
}
print!("Checking cache accessibility... ");
let cache_dir = dirs::cache_dir().unwrap_or_else(|| path.to_path_buf()).join("morph-cli");
if std::fs::create_dir_all(&cache_dir).is_ok() {
println!("{}", terminal::success_prefix());
} else {
println!("{}", terminal::error_prefix());
println!(" Cannot write to cache directory: {}", cache_dir.display());
has_errors = true;
}
print!("Checking session directory... ");
let morph_dir = path.join(".morph-cli");
if morph_dir.exists() && !morph_dir.is_dir() {
println!("{}", terminal::error_prefix());
println!(" .morph-cli exists but is not a directory. Please remove it or rename it.");
has_errors = true;
} else {
println!("{}", terminal::success_prefix());
}
print!("Checking workspace detection... ");
let workspace = detect_workspaces(path);
if workspace.is_workspace() {
println!("{} ({} packages)", terminal::success_prefix(), workspace.packages.len());
} else {
println!("{} (no workspace detected)", terminal::success_prefix());
}
print!("Checking git repository... ");
let git_check = Command::new("git").arg("rev-parse").arg("--is-inside-work-tree").current_dir(path).output();
match git_check {
Ok(output) if output.status.success() => println!("{}", terminal::success_prefix()),
_ => {
println!("{}", terminal::warning_prefix());
println!(" Not a git repository or git not installed. Version control is strongly recommended.");
}
}
print!("Checking parser availability... ");
println!("{}", terminal::success_prefix());
println!();
if has_errors {
terminal::print_info("Health checks completed with errors. Please review the output above.");
} else {
println!("{} All health checks passed! Repository is ready for morph-cli.", terminal::success_prefix());
}
Ok(())
}