use anyhow::{bail, Result};
use std::process::Command;

use crate::git::cli::GitCli;

impl GitCli {
    pub fn tags(&self, repo_path: &str) -> Result<Vec<crate::app::state::TagEntry>> {
        let output = Command::new("git")
            .args([
                "-C",
                repo_path,
                "for-each-ref",
                "refs/tags",
                "--format=%(refname:strip=2)|%(objectname)|%(subject)|%(taggername)",
            ])
            .output()?;

        if !output.status.success() {
            bail!(
                "git tag --list failed: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        let text = String::from_utf8_lossy(&output.stdout);
        let mut tags = Vec::new();
        for line in text.lines() {
            let parts: Vec<&str> = line.splitn(4, '|').collect();
            if parts.len() == 4 {
                tags.push(crate::app::state::TagEntry {
                    name: parts[0].to_string(),
                    message: parts[2].to_string(),
                });
            }
        }
        Ok(tags)
    }

}
