use crate::model::{Conversation, Message, UpdateItem};

#[derive(Debug, Clone)]
pub struct Event {
    pub data: EventData,
    pub update_seq: u64,
}

#[derive(Debug, Clone)]
pub enum EventData {
    ConversationChanged(Conversation),
    MessageReceived(Conversation, Message),
}

impl From<UpdateItem> for Event {
    fn from(update: UpdateItem) -> Self {
        match update {
            UpdateItem {
                conversation: Some(conversation),
                message: None,
                ..
            } => Event {
                data: EventData::ConversationChanged(conversation),
                update_seq: update.seq,
            },

            UpdateItem {
                conversation: Some(conversation),
                message: Some(message),
                ..
            } => Event {
                data: EventData::MessageReceived(conversation, message),
                update_seq: update.seq,
            },

            _ => panic!("Invalid update item: {:?}", update),
        }
    }
}
