use anyhow::Result;

/// Execute a command inside a running container.
pub fn handle_exec(service: &str, cmd: &[String]) -> Result<()> {
    // Get the container name from service name
    let container = format!("orca-{service}");

    // Check if container is running
    let output = std::process::Command::new("docker")
        .args(["inspect", "--format", "{{.State.Running}}", &container])
        .output()?;

    if !output.status.success() || String::from_utf8_lossy(&output.stdout).trim() != "true" {
        anyhow::bail!("Container '{container}' is not running");
    }

    let mut docker_cmd = std::process::Command::new("docker");
    docker_cmd.arg("exec");
    if std::io::IsTerminal::is_terminal(&std::io::stdout()) {
        docker_cmd.arg("-it");
    }
    let status = docker_cmd.arg(&container).args(cmd).status()?;

    if !status.success() {
        anyhow::bail!("Command exited with status: {status}");
    }

    Ok(())
}
