use std::io;
use std::process::{Command, Stdio};

/// Checks if the ripgrep (rg) command-line tool is available in the system's PATH.
///
/// Returns Ok(()) if rg is found, otherwise returns an Err with a message.
pub fn check_ripgrep_exists() -> Result<(), String> {
    match Command::new("rg")
        .arg("--version")
        .stdout(Stdio::null()) // Suppress stdout
        .stderr(Stdio::null()) // Suppress stderr
        .status()
    {
        Ok(status) => {
            if status.success() {
                Ok(())
            } else {
                Err(format!(
                    "ripgrep (rg) command failed with status: {status}"
                ))
            }
        }
        Err(e) => {
            if e.kind() == io::ErrorKind::NotFound {
                Err("ripgrep (rg) command not found in PATH. Please install ripgrep.".to_string())
            } else {
                Err(format!("Failed to execute ripgrep (rg) command: {e}"))
            }
        }
    }
}

/// Checks all necessary prerequisites for the tool.
/// Currently, only checks for ripgrep.
pub fn check_prerequisites() -> Result<(), String> {
    check_ripgrep_exists()
}
