use anyhow::{bail, Context, Result};

use crate::util::discover_repo;

pub fn run(name: Option<String>, delete: Option<String>, all: bool) -> Result<()> {
    let cwd = std::env::current_dir()?;
    let repo = discover_repo(&cwd)?;
    let workdir = repo
        .workdir()
        .context("cannot run branch in a bare repository")?
        .to_path_buf();
    let git_dir = repo.git_dir().to_path_buf();
    let git_exe = gix::path::env::exe_invocation();

    let mut cmd = std::process::Command::new(git_exe);
    cmd.arg("branch");

    if let Some(d) = &delete {
        cmd.arg("-d").arg(d);
    } else if let Some(n) = &name {
        cmd.arg(n);
    } else if all {
        cmd.arg("-a");
    }

    cmd.current_dir(&workdir);
    cmd.env("GIT_DIR", &git_dir);

    let output = cmd.output().context("failed to run git branch")?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!("branch failed: {}", stderr.trim());
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    print!("{}", stdout);
    Ok(())
}
