use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// FA(3) TRodzajFaktury values.
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum InvoiceKind {
    #[serde(rename = "VAT")]
    Vat,
    #[serde(rename = "KOR")]
    Kor,
    #[serde(rename = "ZAL")]
    Zal,
    #[serde(rename = "ROZ")]
    Roz,
    #[serde(rename = "UPR")]
    Upr,
    #[serde(rename = "KOR_ZAL")]
    KorZal,
    #[serde(rename = "KOR_ROZ")]
    KorRoz,
}

impl FromStr for InvoiceKind {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "VAT" => Ok(Self::Vat),
            "KOR" => Ok(Self::Kor),
            "ZAL" => Ok(Self::Zal),
            "ROZ" => Ok(Self::Roz),
            "UPR" => Ok(Self::Upr),
            "KOR_ZAL" => Ok(Self::KorZal),
            "KOR_ROZ" => Ok(Self::KorRoz),
            _ => Err(()),
        }
    }
}

impl InvoiceKind {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Vat => "VAT",
            Self::Kor => "KOR",
            Self::Zal => "ZAL",
            Self::Roz => "ROZ",
            Self::Upr => "UPR",
            Self::KorZal => "KOR_ZAL",
            Self::KorRoz => "KOR_ROZ",
        }
    }
}

impl std::fmt::Display for InvoiceKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}
