use std::time::SystemTime;

use crate::data::keyed::Keyed;

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) struct Personalization {
    pub(crate) id: u32,
    #[allow(dead_code)]
    pub(crate) variation_id: u32,
    pub(crate) assignment_time: SystemTime,
}

impl Personalization {
    pub(crate) fn new(id: u32, variation_id: u32, assignment_time: SystemTime) -> Self {
        return Personalization {
            id,
            variation_id,
            assignment_time,
        };
    }
}

impl Keyed<u32> for Personalization {
    fn key(&self) -> &u32 {
        return &self.id;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new() {
        const ID: u32 = 123;
        const VAR_ID: u32 = 456;
        let assignment_time = SystemTime::UNIX_EPOCH + std::time::Duration::from_millis(12345);
        let personalization = Personalization::new(ID, VAR_ID, assignment_time);
        assert_eq!(ID, personalization.id);
        assert_eq!(VAR_ID, personalization.variation_id);
        assert_eq!(assignment_time, personalization.assignment_time);
    }

    #[test]
    fn test_key() {
        const ID: u32 = 98;
        let personalization = Personalization::new(ID, 0, SystemTime::UNIX_EPOCH);
        assert_eq!(ID, *personalization.key());
    }
}
