//! 前后端事件定义
//! 定义从后端发送到前端的所有事件

use serde::{Deserialize, Serialize};
use super::{DeviceInfo, ChatMessage, FileTransfer};

/// 事件名称常量
pub mod event_names {
    /// 发现新设备
    pub const DEVICE_FOUND: &str = "device_found";
    /// 设备离线
    pub const DEVICE_LOST: &str = "device_lost";
    /// 收到消息
    pub const MESSAGE_RECEIVED: &str = "message_received";
    /// 收到文件
    pub const FILE_RECEIVED: &str = "file_received";
    /// 传输进度更新
    pub const TRANSFER_PROGRESS: &str = "transfer_progress";
    /// WebSocket 已连接
    pub const WEBSOCKET_CONNECTED: &str = "websocket_connected";
    /// WebSocket 已断开
    pub const WEBSOCKET_DISCONNECTED: &str = "websocket_disconnected";
    /// 消息状态更新
    pub const MESSAGE_STATUS_UPDATED: &str = "message_status_updated";
}

/// 设备发现事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeviceFoundEvent {
    /// 发现的设备信息
    pub device: DeviceInfo,
}

/// 设备离线事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeviceLostEvent {
    /// 离线设备 ID
    pub device_id: String,
}

/// 消息接收事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MessageReceivedEvent {
    /// 接收到的消息
    pub message: ChatMessage,
}

/// 文件接收事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileReceivedEvent {
    /// 文件传输信息
    pub transfer: FileTransfer,
}

/// 传输进度事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferProgressEvent {
    /// 文件 ID
    pub file_id: String,
    /// 当前进度 (0.0 - 1.0)
    pub progress: f64,
    /// 已传输字节数
    pub transferred_bytes: u64,
    /// 总字节数
    pub total_bytes: u64,
    /// 传输状态
    pub status: super::TransferStatus,
    /// 文件名（可选，用于接收方显示）
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_name: Option<String>,
    /// 本地路径（可选，用于接收方）
    #[serde(skip_serializing_if = "Option::is_none")]
    pub local_path: Option<String>,
}

/// WebSocket 连接事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WebSocketConnectedEvent {
    /// 连接的设备 ID
    pub device_id: String,
}

/// WebSocket 断开事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WebSocketDisconnectedEvent {
    /// 断开的设备 ID
    pub device_id: String,
    /// 断开原因
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

/// 消息状态更新事件
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MessageStatusUpdatedEvent {
    /// 消息 ID
    pub message_id: String,
    /// 新状态
    pub status: super::MessageStatus,
}
