//! 文件传输数据结构

use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// 文件传输状态
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum TransferStatus {
    /// 等待中
    Pending,
    /// 传输中
    Transferring,
    /// 已完成
    Completed,
    /// 失败
    Failed,
    /// 已取消
    Cancelled,
}

/// 文件传输信息
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileTransfer {
    /// 文件传输唯一标识符
    pub file_id: String,
    /// 文件名
    pub name: String,
    /// 文件大小 (字节)
    pub size: u64,
    /// 传输进度 (0.0 - 1.0)
    pub progress: f64,
    /// 传输状态
    pub status: TransferStatus,
    /// MIME 类型
    pub mime_type: String,
    /// 发送者设备 ID
    pub from_device: String,
    /// 接收者设备 ID
    pub to_device: String,
    /// 本地文件路径 (发送时为源路径，接收时为保存路径)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub local_path: Option<String>,
    /// 创建时间戳
    pub created_at: i64,
    /// 完成时间戳
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<i64>,
    /// 已传输字节数
    pub transferred_bytes: u64,
}

impl FileTransfer {
    /// 创建新的文件传输记录 (发送)
    pub fn new_outgoing(
        name: String,
        size: u64,
        mime_type: String,
        from_device: String,
        to_device: String,
        local_path: String,
    ) -> Self {
        Self {
            file_id: Uuid::new_v4().to_string(),
            name,
            size,
            progress: 0.0,
            status: TransferStatus::Pending,
            mime_type,
            from_device,
            to_device,
            local_path: Some(local_path),
            created_at: chrono::Utc::now().timestamp(),
            completed_at: None,
            transferred_bytes: 0,
        }
    }

    /// 创建新的文件传输记录 (接收)
    pub fn new_incoming(
        file_id: String,
        name: String,
        size: u64,
        mime_type: String,
        from_device: String,
        to_device: String,
    ) -> Self {
        Self {
            file_id,
            name,
            size,
            progress: 0.0,
            status: TransferStatus::Pending,
            mime_type,
            from_device,
            to_device,
            local_path: None,
            created_at: chrono::Utc::now().timestamp(),
            completed_at: None,
            transferred_bytes: 0,
        }
    }

    /// 更新传输进度
    pub fn update_progress(&mut self, transferred_bytes: u64) {
        self.transferred_bytes = transferred_bytes;
        if self.size > 0 {
            self.progress = transferred_bytes as f64 / self.size as f64;
        }
        if self.progress >= 1.0 {
            self.progress = 1.0;
            self.status = TransferStatus::Completed;
            self.completed_at = Some(chrono::Utc::now().timestamp());
        } else {
            self.status = TransferStatus::Transferring;
        }
    }

    /// 标记失败
    pub fn mark_failed(&mut self) {
        self.status = TransferStatus::Failed;
        self.completed_at = Some(chrono::Utc::now().timestamp());
    }

    /// 标记取消
    pub fn mark_cancelled(&mut self) {
        self.status = TransferStatus::Cancelled;
        self.completed_at = Some(chrono::Utc::now().timestamp());
    }
}

/// 文件上传请求
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileUploadRequest {
    /// 文件 ID
    pub file_id: String,
    /// 文件名
    pub file_name: String,
    /// 文件大小
    pub file_size: u64,
    /// MIME 类型
    pub mime_type: String,
    /// 发送者设备 ID
    pub from_device: String,
}

/// 文件元数据
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileMetadata {
    /// 文件 ID
    pub file_id: String,
    /// 文件名
    pub name: String,
    /// 文件大小
    pub size: u64,
    /// MIME 类型
    pub mime_type: String,
    /// 缩略图 (base64, 可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumbnail: Option<String>,
    /// 文件路径
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
}
