use serde::{Deserialize, Serialize};

use crate::{
    bare::GitBare,
    cmd::command::GitCommandParams,
    errors::{GitError, GitResult},
};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TagDeleteParams {
    pub name: String,
}

impl GitBare {
    pub fn tag_delete(&self, params: TagDeleteParams) -> GitResult<()> {
        let cmd_params = GitCommandParams::new(vec![
            "tag".to_string(),
            "-d".to_string(),
            params.name.clone(),
        ])
        .trusted()
        .unchecked();

        let output = self.git_command_with(cmd_params)?;
        if !output.success {
            let stderr = output.stderr_lossy();
            if stderr.contains("not found") {
                return Err(GitError::RefNotFound(params.name));
            }
            return Err(GitError::CommandFailed {
                status_code: output.status_code,
                stderr,
            });
        }

        Ok(())
    }
}
