use serde_repr::{Deserialize_repr, Serialize_repr};

/// Options for the video content and ad play mode.
#[derive(Serialize_repr, Deserialize_repr, Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum PlaybackCessationMode {
    /// On video completion or when user exits
    OnCompletion = 1,

    /// On page exit
    OnExit = 2,

    /// On float
    OnFloat = 3,
}

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

    #[test]
    fn test_all_valid_values() {
        // Test all valid PlaybackCessationMode values (1-3)
        for value in 1..=3 {
            let json = format!("{}", value);
            let result: Result<PlaybackCessationMode, _> = serde_json::from_str(&json);
            assert!(
                result.is_ok(),
                "Valid value {} should deserialize successfully",
                value
            );
        }
    }
    #[test]
    fn test_invalid_value_zero() {
        let json = "0";
        let result: Result<PlaybackCessationMode, _> = serde_json::from_str(json);
        assert!(
            result.is_err(),
            "Value 0 is not a valid PlaybackCessationMode and should fail deserialization"
        );
    }
    #[test]
    fn test_invalid_value_out_of_range() {
        let json = "99";
        let result: Result<PlaybackCessationMode, _> = serde_json::from_str(json);
        assert!(
            result.is_err(),
            "Value 99 is out of range and should fail deserialization"
        );
    }
    #[test]
    fn test_invalid_value_negative() {
        let json = "-1";
        let result: Result<PlaybackCessationMode, _> = serde_json::from_str(json);
        assert!(
            result.is_err(),
            "Negative values should fail deserialization"
        );
    }
    #[test]
    fn test_serialization_roundtrip() {
        let values = [
            PlaybackCessationMode::OnCompletion,
            PlaybackCessationMode::OnExit,
            PlaybackCessationMode::OnFloat,
        ];

        for original in values {
            let json = serde_json::to_string(&original).unwrap();
            let deserialized: PlaybackCessationMode = serde_json::from_str(&json).unwrap();
            assert_eq!(
                original, deserialized,
                "Serialization roundtrip failed for {:?}",
                original
            );
        }
    }
}
