bzzz-cli 0.1.0

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

use anyhow::Result;

use crate::run_registry::RunRegistry;

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

    match registry.get(id)? {
        Some(mut state) => {
            println!("📊 Status for Run: {}", id);

            // Check if process is alive (for running status)
            if state.status == bzzz_core::RunStatus::Running {
                if let Some(pid) = state.pid {
                    if state.is_process_alive() {
                        println!("  PID: {} (running)", pid);
                    } else {
                        println!("  PID: {} (exited)", pid);
                        // Update status to completed
                        state.status = bzzz_core::RunStatus::Completed;
                        registry.update_status(id, state.status)?;
                    }
                } else {
                    println!("  PID: N/A");
                }
            } else {
                if let Some(pid) = state.pid {
                    println!("  PID: {}", pid);
                }
            }

            println!("  Status: {:?}", state.status);
            println!("  Runtime: {:?}", state.runtime_kind);
            println!("  Spec: {}", state.spec_path.display());

            // Calculate elapsed time
            let elapsed = state.started_at.elapsed()?;
            let secs = elapsed.as_secs();
            let mins = secs / 60;
            let hours = mins / 60;

            if hours > 0 {
                println!("  Elapsed: {}h {}m {}s", hours, mins % 60, secs % 60);
            } else if mins > 0 {
                println!("  Elapsed: {}m {}s", mins, secs % 60);
            } else {
                println!("  Elapsed: {}s", secs);
            }

            println!("  Working Dir: {}", state.working_dir.display());
        }
        None => {
            println!("❌ Run not found: {}", id);
            println!("  Tip: Use `bzzz list` to see all runs");
        }
    }

    Ok(())
}