use std::process::Command;

pub fn run(db: Option<&str>) -> anyhow::Result<()> {
    let mut cmd = Command::new("rok-tui");

    if let Some(url) = db {
        cmd.env("DATABASE_URL", url);
    }

    let status = cmd.status();

    match status {
        Ok(s) if s.success() => Ok(()),
        Ok(s) => anyhow::bail!("rok-tui exited with status {s}"),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            anyhow::bail!(
                "rok-tui binary not found — install it with:\n  cargo install --path crates/rok-tui"
            )
        }
        Err(e) => Err(e.into()),
    }
}
