use crate::supports::enums;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Recommendation {
    Approve,
    Decline,
    Review,
    //usado para quando enviamos pedidos de backfill dentro da plataforma
    None,
}

pub type DecisionType = Recommendation;

impl<'de> Deserialize<'de> for Recommendation {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        match s.to_lowercase().as_str() {
            "approve" => Ok(Recommendation::Approve),
            "decline" => Ok(Recommendation::Decline),
            "review" => Ok(Recommendation::Review),
            "none" => Ok(Recommendation::None),
            _ => Err(serde::de::Error::custom(format!(
                "Invalid recommendation: {}",
                s
            ))),
        }
    }
}

impl Recommendation {
    pub fn name(&self) -> String {
        enums::to_string_infallible(self)
    }
}

impl Display for Recommendation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}
