// file: kb_lib/src/db/types/observed_token_status.rs

//! Observed token status.

/// Local status of one observed token.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ObservedTokenStatus {
    /// Newly discovered token.
    New,
    /// Token currently tracked.
    Active,
    /// Token ignored by local filtering rules.
    Ignored,
    /// Token marked as suspicious or blocked.
    Blocked,
}

impl ObservedTokenStatus {
    /// Converts the status to its stable integer representation.
    pub fn to_i16(self) -> i16 {
        match self {
            Self::New => return 0,
            Self::Active => return 1,
            Self::Ignored => return 2,
            Self::Blocked => return 3,
        }
    }

    /// Restores a status from its stable integer representation.
    pub fn from_i16(value: i16) -> Result<Self, crate::Error> {
        match value {
            0 => return Ok(Self::New),
            1 => return Ok(Self::Active),
            2 => return Ok(Self::Ignored),
            3 => return Ok(Self::Blocked),
            _ => {
                return Err(crate::Error::Db(format!(
                    "invalid ObservedTokenStatus value: {}",
                    value
                )));
            },
        }
    }
}
