use super::conversation::ConversationID;
use chrono::NaiveDateTime;
use serde::Serialize;
use uuid::Uuid;

#[derive(Debug, Clone, Serialize)]
pub struct OutgoingMessage {
    #[serde(skip)]
    pub guid: Uuid,

    #[serde(skip)]
    pub date: NaiveDateTime,

    #[serde(rename = "body")]
    pub text: String,

    #[serde(rename = "guid")]
    pub conversation_id: ConversationID,

    #[serde(rename = "fileTransferGUIDs")]
    pub file_transfer_guids: Vec<String>,
}

impl OutgoingMessage {
    pub fn builder() -> OutgoingMessageBuilder {
        OutgoingMessageBuilder::new()
    }
}

#[derive(Default)]
pub struct OutgoingMessageBuilder {
    guid: Option<Uuid>,
    text: Option<String>,
    conversation_id: Option<ConversationID>,
    file_transfer_guids: Option<Vec<String>>,
}

impl OutgoingMessageBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn guid(mut self, guid: Uuid) -> Self {
        self.guid = Some(guid);
        self
    }

    pub fn text(mut self, text: String) -> Self {
        self.text = Some(text);
        self
    }

    pub fn conversation_id(mut self, conversation_id: ConversationID) -> Self {
        self.conversation_id = Some(conversation_id);
        self
    }

    pub fn file_transfer_guids(mut self, file_transfer_guids: Vec<String>) -> Self {
        self.file_transfer_guids = Some(file_transfer_guids);
        self
    }

    pub fn build(self) -> OutgoingMessage {
        OutgoingMessage {
            guid: self.guid.unwrap_or_else(Uuid::new_v4),
            text: self.text.unwrap(),
            conversation_id: self.conversation_id.unwrap(),
            file_transfer_guids: self.file_transfer_guids.unwrap_or_default(),
            date: chrono::Utc::now().naive_utc(),
        }
    }
}
