use anyhow::Result;
use tracing::info;

/// Reload: shutdown daemon, restart, and redeploy all services.
///
/// Re-launches the server in the SAME working directory the daemon was
/// originally started in (recorded in `~/.orca/orca.cwd`), not the cwd of
/// the user who runs `orca reload`. This guarantees `cluster.toml` and the
/// `services/` directory continue to resolve correctly.
pub async fn handle_reload() -> Result<()> {
    let cwd = super::daemon::read_cwd()
        .unwrap_or_else(|| std::env::current_dir().expect("could not determine cwd"));

    // Read current server args from the running process
    let pid = super::daemon::read_pid();

    if let Some(pid) = pid {
        info!("Stopping current daemon (PID: {pid})");
        super::daemon::handle_shutdown()?;
        // Wait for clean shutdown
        tokio::time::sleep(std::time::Duration::from_secs(2)).await;
    }

    // Restart the server daemon
    let exe = std::env::current_exe()?;
    info!("Restarting orca server...");
    let output = std::process::Command::new(&exe)
        .args(["server", "--daemon"])
        .current_dir(&cwd)
        .output()?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("Failed to restart server: {stderr}");
    }
    print!("{}", String::from_utf8_lossy(&output.stdout));

    // Wait for API to be ready
    tokio::time::sleep(std::time::Duration::from_secs(3)).await;

    // Redeploy all services (try services/ dir first, fall back to services.toml)
    let deploy_arg = if cwd.join("services").is_dir() {
        "services"
    } else {
        "services.toml"
    };
    info!("Redeploying services from {deploy_arg}...");
    let output = std::process::Command::new(&exe)
        .args(["deploy", "--file", deploy_arg])
        .current_dir(&cwd)
        .output()?;

    print!("{}", String::from_utf8_lossy(&output.stdout));
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("Deploy failed: {stderr}");
    }

    println!("Reload complete.");
    Ok(())
}
