//! Internal chat domain events emitted after durable writes and relayed to WebSocket clients.
//!
//! Events are persisted in the transactional outbox and published to room topics; the WSS layer
//! maps envelopes to [`openapi-wss.yaml`](../../../openapi-wss.yaml) `ChatWs*Event` payloads.

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

use crate::dto::results::{MessageView, ReactionSummary, RoomView};
use crate::ids::{MessageId, RoomId, UserId};

/// Versioned domain event for a single room, suitable for outbox storage and fan-out.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatEventEnvelope {
    pub event_id: Uuid,
    pub room_id: RoomId,
    pub room_seq: Option<i64>,
    pub occurred_at: DateTime<Utc>,
    pub payload: ChatEventPayload,
}

/// Tagged union of chat mutations that can be serialized to JSON for outbox and relay.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ChatEventPayload {
    RoomCreated { room: RoomView },
    RoomUpdated { room: RoomView },
    RoomArchived { room: RoomView },
    MessageCreated { message: MessageView },
    MessageUpdated { message: MessageView },
    MessageDeleted { message_id: MessageId },
    ReadUpdated { user_id: UserId, read_seq: i64 },
    MembersUpdated { version: i64 },
    ReactionUpdated { summary: ReactionSummary },
}
