use serde::{Deserialize, Serialize};

use crate::{
    action::{ActionError, Context},
    action_impl::{spawn, ActionImpl},
};

/// Configure git user.name and user.email.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GitConfig;

impl ActionImpl for GitConfig {
    fn execute(&self, context: &mut Context) -> Result<(), ActionError> {
        let snippets = [
            "git config --global user.name 'Ambient CI'",
            "git config --global user.email ambient@example.com",
        ];

        for snippet in snippets {
            spawn(context, &["bash", "-c", snippet])?;
        }

        Ok(())
    }
}

/// Errors from the Pwd action.
#[derive(Debug, thiserror::Error)]
pub enum GitError {}

impl From<GitError> for ActionError {
    fn from(value: GitError) -> Self {
        Self::GitConfig(value)
    }
}
