use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::event::{RoomInfo, UserInfo};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReactionEventType {
    Added,
    Removed,
    BatchUpdated,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ReactionEvent {
    #[serde(rename = "reaction.added")]
    Added(ReactionAddedService),
    #[serde(rename = "reaction.removed")]
    Removed(ReactionRemovedService),
    #[serde(rename = "reaction.batch_updated")]
    BatchUpdated(ReactionBatchUpdatedService),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReactionAddedService {
    pub id: Uuid,
    pub room: RoomInfo,
    pub message: Uuid,
    pub user: UserInfo,
    pub emoji: String,
    pub created_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReactionRemovedService {
    pub id: Uuid,
    pub room: RoomInfo,
    pub message: Uuid,
    pub user: UserInfo,
    pub emoji: String,
    pub removed_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReactionBatchUpdatedService {
    pub room: RoomInfo,
    pub message: Uuid,
    pub reactions: Vec<ReactionGroup>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReactionGroup {
    pub emoji: String,
    pub count: i64,
    pub reacted_by_me: bool,
    pub users: Vec<UserInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReactionAddClient {
    pub room: RoomInfo,
    pub message: Uuid,
    pub emoji: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReactionRemoveClient {
    pub room: RoomInfo,
    pub message: Uuid,
    pub emoji: String,
}
