"""聊天记录导出相关数据模型。"""
from __future__ import annotations

from typing import Literal, Optional

from pydantic import BaseModel, Field


# ---------------------------------------------------------------------------
# 导出请求（POST /api/messages/export 异步用）
# ---------------------------------------------------------------------------
class ExportRequest(BaseModel):
    """导出请求（POST /api/messages/export 异步用）。

    limit > 1000 或 include_media=true 时应走异步端点；
    同步 GET /api/messages/export 仅服务 limit ≤ 1000 的纯文本导出。
    """

    talker: str = Field(..., description="会话 wxid 或群 id")
    format: Literal["html", "csv", "json", "txt"] = Field(
        "html", description="导出格式：html / csv / json / txt"
    )
    limit: int = Field(1000, ge=1, le=100000, description="导出消息数上限")
    include_media: bool = Field(False, description="是否打包媒体文件（zip）")
    media_inline: bool = Field(
        False, description="媒体是否内联（仅 HTML，将图片 base64 嵌入）"
    )
    start_time: Optional[int] = Field(None, description="起始时间戳（Unix 秒，含）")
    end_time: Optional[int] = Field(None, description="结束时间戳（Unix 秒，含）")


# ---------------------------------------------------------------------------
# 导出任务状态
# ---------------------------------------------------------------------------
class ExportTaskStatus(BaseModel):
    """导出任务状态。

    状态机：pending → running → completed / failed / cancelled。
    completed 时 download_url 字段有效；expires_at 之后下载链接失效。
    """

    task_id: str = Field(description="任务 ID（UUID）")
    status: Literal[
        "pending", "running", "completed", "failed", "cancelled"
    ] = Field("pending", description="任务状态")
    progress: float = Field(
        0.0, ge=0.0, le=1.0, description="进度 0-1（processed/total）"
    )
    total: int = Field(0, description="待处理消息总数（运行中逐步更新）")
    processed: int = Field(0, description="已处理消息数")
    download_url: Optional[str] = Field(
        None, description="下载链接（status=completed 时有效）"
    )
    expires_at: Optional[int] = Field(
        None, description="下载链接过期时间戳（默认 2 小时后）"
    )
    error_message: Optional[str] = Field(
        None, description="失败原因（status=failed 时有效）"
    )
