#![allow(dead_code)]
//! Node operation commands

use crate::dev::{DevState, NodeManager, RestartResult};
use anyhow::Result;
use std::path::Path;

/// Trigger hot-reload
pub async fn hot_reload(state: &mut DevState) -> Result<()> {
    println!("\n╭─────────────────────────────────────────╮");
    println!("│  Hot Reload                              │");
    println!("╰─────────────────────────────────────────╯\n");
    println!("  Triggering hot-reload...");

    if !Path::new("Cargo.toml").exists() {
        println!("  ✗ No Cargo.toml found in current directory");
        println!("  Make sure you're in a mecha10 project directory");
        return Ok(());
    }

    println!("  Building project...");

    let restart_result = NodeManager::restart_all(&state.project_config, &mut state.process_tracker)?;

    match restart_result {
        RestartResult::Success {
            build_result,
            start_result,
        } => {
            if let crate::dev::BuildResult::Success { warning_count } = build_result {
                println!("  ✓ Build successful");
                if warning_count > 0 {
                    println!("  ⚠ {} warning(s) during build", warning_count);
                }
            }

            println!("\n  Restarting nodes...\n");

            for (name, pid) in &start_result.started {
                println!("  ✓ {} restarted (PID: {})", name, pid);
            }

            for (name, error) in &start_result.failed {
                println!("  ✗ {} - {}", name, error);
            }

            println!("\n  Hot-reload complete!");
        }
        RestartResult::BuildFailed(build_result) => {
            if let crate::dev::BuildResult::Failed { errors } = build_result {
                println!("  ✗ Build failed\n");
                if !errors.is_empty() {
                    println!("  Errors:");
                    for error in errors.iter().take(5) {
                        println!("    {}", error);
                        println!();
                    }
                }
                println!("  Run 'cargo build --all' for full output");
            }
        }
    }

    Ok(())
}
