use crate::utils::ApplicationError;

#[derive(sqlx::Type, Debug, Clone, Copy, PartialEq)]
#[sqlx(type_name = "role", rename_all = "lowercase")]
pub enum Role {
    Staff = 1,
    AreaManager = 10,
    Admin = 100,
}

impl TryFrom<u8> for Role {
    type Error = ApplicationError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Role::Staff),
            10 => Ok(Role::AreaManager),
            100 => Ok(Role::Admin),
            _ => Err(ApplicationError::UnsupportedEnumValue {
                value: value.to_string(),
                enum_name: String::from("Role"),
            }),
        }
    }
}

impl Default for Role {
    fn default() -> Self {
        Self::Staff
    }
}
