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

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

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RoomEventType {
    Created,
    Deleted,
    Renamed,
    TopicUpdated,
    SettingsUpdated,
    Moved,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum RoomEvent {
    #[serde(rename = "room.created")]
    Created(RoomCreatedService),
    #[serde(rename = "room.deleted")]
    Deleted(RoomDeletedService),
    #[serde(rename = "room.renamed")]
    Renamed(RoomRenamedService),
    #[serde(rename = "room.moved")]
    Moved(RoomMovedService),
    #[serde(rename = "room.topic_updated")]
    TopicUpdated(RoomTopicUpdatedService),
    #[serde(rename = "room.settings_updated")]
    SettingsUpdated(RoomSettingsUpdatedService),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomCreatedService {
    pub room: RoomInfo,
    pub workspace: WorkspaceInfo,
    pub public: bool,
    pub category: Option<Uuid>,
    pub created_by: UserInfo,
    pub created_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomDeletedService {
    pub room: RoomInfo,
    pub workspace: WorkspaceInfo,
    pub deleted_by: UserInfo,
    pub deleted_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomRenamedService {
    pub room: RoomInfo,
    pub workspace: WorkspaceInfo,
    pub old_name: String,
    pub new_name: String,
    pub renamed_by: UserInfo,
    pub renamed_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomMovedService {
    pub room: RoomInfo,
    pub workspace: WorkspaceInfo,
    pub old_category: Option<Uuid>,
    pub new_category: Option<Uuid>,
    pub moved_by: UserInfo,
    pub moved_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomTopicUpdatedService {
    pub room: RoomInfo,
    pub workspace: WorkspaceInfo,
    pub old_topic: Option<String>,
    pub new_topic: Option<String>,
    pub updated_by: UserInfo,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomSettingsUpdatedService {
    pub room: RoomInfo,
    pub workspace: WorkspaceInfo,
    pub slowmode_seconds: Option<i32>,
    pub nsfw: bool,
    pub default_auto_archive_duration: Option<i32>,
    pub updated_by: UserInfo,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomCreateClient {
    pub workspace: WorkspaceInfo,
    pub room_name: String,
    pub public: bool,
    pub category: Option<Uuid>,
    pub ai_enabled: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomUpdateClient {
    pub room_name: Option<String>,
    pub public: Option<bool>,
    pub category: Option<Uuid>,
    pub topic: Option<String>,
    pub slowmode_seconds: Option<i32>,
    pub nsfw: Option<bool>,
    pub default_auto_archive_duration: Option<i32>,
    pub ai_enabled: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomDeleteClient {}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomLoadClient {
    pub room: RoomInfo,
}
