use serde::{Deserialize, Serialize};

use crate::{
    bare::GitBare,
    cmd::{
        oid::ObjectId,
        tag::{tag_delete::TagDeleteParams, tag_init::TagInitParams},
        tagger::GitTagger,
    },
    errors::GitResult,
};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TagUpdateMessageParams {
    pub name: String,
    pub message: String,
    pub tagger: GitTagger,
    pub force: bool,
}

impl GitBare {
    pub fn tag_update_message(
        &self,
        params: TagUpdateMessageParams,
    ) -> GitResult<ObjectId> {
        let current_info = self.tag_info(params.name.clone())?;
        self.tag_delete(TagDeleteParams {
            name: params.name.clone(),
        })?;
        let init_params = TagInitParams {
            name: params.name.clone(),
            target: current_info.target.clone(),
            message: Some(params.message.clone()),
            tagger: Some(params.tagger.clone()),
            force: params.force,
        };

        self.tag_init(init_params)
    }
}
