use clap::Subcommand;
use std::error::Error;

#[derive(Clone)]
pub enum Protocol {
    Json,
}

impl Protocol {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Json => "JSON",
        }
    }
}

impl clap::ValueEnum for Protocol {
    fn value_variants<'a>() -> &'a [Self] {
        &[Self::Json]
    }

    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
        match self {
            Self::Json => Some(clap::builder::PossibleValue::new("JSON")),
        }
    }
}

#[derive(Subcommand)]
pub enum Integrations {
    /// add a new integration to your Ordinary project
    Add {
        /// name of the integration
        name: String,

        /// url endpoint for integration
        endpoint: String,

        /// protocol for the integration
        protocol: Protocol,
    },
}

impl Integrations {
    pub fn handle(&self, project: &str) -> Result<(), Box<dyn Error>> {
        match self {
            Self::Add {
                name,
                endpoint,
                protocol,
            } => {
                ordinary_modify::add_integration(project, name, endpoint, protocol.as_str())?;
            }
        }

        Ok(())
    }
}
