use crate::{DbnestError, Result};
use std::process::Command;

pub fn ensure_docker_available() -> Result<()> {
    let version = Command::new("docker")
        .arg("--version")
        .output()
        .map_err(|_| DbnestError::DockerNotAvailable)?;

    if !version.status.success() {
        return Err(DbnestError::DockerNotAvailable);
    }

    let info = Command::new("docker")
        .arg("info")
        .output()
        .map_err(|_| DbnestError::DockerNotAvailable)?;

    if info.status.success() {
        return Ok(());
    }

    Err(DbnestError::DockerCommandFailed {
        command: "docker info".into(),
        stderr: String::from_utf8_lossy(&info.stderr).trim().to_string(),
        hint: crate::runtime::docker::util::docker_failure_hint(
            &String::from_utf8_lossy(&info.stderr),
            "Docker is installed but the daemon is not reachable. Start Docker Desktop or the Docker daemon, and check user permissions.",
        ),
    })
}
