bzzz-cli 0.1.0

Bzzz CLI - Command line interface for Agent orchestration
//! Stop command

use anyhow::Result;

use crate::run_registry::RunRegistry;

pub async fn execute(id: &str, force: bool) -> Result<()> {
    let registry = RunRegistry::default();

    match registry.get(id)? {
        Some(state) => {
            println!("🛑 Stopping Run: {}", id);

            // Check if already stopped
            if state.status != bzzz_core::RunStatus::Running {
                println!("  Already stopped: {:?}", state.status);
                return Ok(());
            }

            // Check if process is alive
            if let Some(pid) = state.pid {
                if state.is_process_alive() {
                    // Kill the process
                    let signal = if force { "SIGKILL" } else { "SIGTERM" };
                    println!("  Sending {} to PID {}...", signal, pid);

                    match state.kill_process(force) {
                        Ok(()) => {
                            println!("  Process killed successfully");
                        }
                        Err(e) => {
                            println!("  Warning: Failed to kill process: {}", e);
                        }
                    }
                } else {
                    println!("  Process already exited");
                }
            } else {
                println!("  No PID to kill (run may have been in-memory)");
            }

            // Update status
            registry.mark_stopped(id, if force { "force stop" } else { "graceful stop" })?;
            println!("  Status: Stopped");
        }
        None => {
            println!("❌ Run not found: {}", id);
            println!("  Tip: Use `bzzz list` to see all runs");
        }
    }

    Ok(())
}