use wot_network::Certification as WotCertification;

/// A wrapper around a [`wot_network::Certification`] to track trust amount state during the
/// Berblom search.
#[derive(Debug, Clone)]
pub(crate) struct Certification {
    pub inner: WotCertification,
    pub allowed_amount: u8,
    pub used_amount: u8,
}

impl Certification {
    /// Creates a new Certification from a [`wot_network::Certification`].
    pub fn new(wot_cert: WotCertification) -> Self {
        Self {
            inner: wot_cert,
            allowed_amount: 120,
            used_amount: 0,
        }
    }

    /// Returns the remaining trust capacity that can be directed through this certification.
    pub fn available_amount(&self) -> u8 {
        self.allowed_amount.saturating_sub(self.used_amount)
    }
}

impl PartialEq for Certification {
    fn eq(&self, other: &Self) -> bool {
        self.inner == other.inner
    }
}

impl Eq for Certification {}

impl std::hash::Hash for Certification {
    /// Implement `hash` for Certification, by only using the actual non-chaining certification.
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.inner.hash(state);
    }
}
