"""
小程序 CHAT 模块 Pydantic 模型。

覆盖：对话历史列表、消息查看、发送消息、SSE 流式请求等场景。
"""

from __future__ import annotations

from app.schemas.base import CamelModel


# ---------------------------------------------------------------------------
# 对话历史（CHAT-1）
# ---------------------------------------------------------------------------


class ChatHistoryItem(CamelModel):
    """对话历史列表项。"""

    id: int
    title: str
    customer_name: str | None = None
    last_message: str | None = None
    timestamp: str  # ISO 8601，最后消息时间
    unread_count: int = 0


class ChatHistoryResponse(CamelModel):
    """CHAT-1 对话历史列表响应。"""

    items: list[ChatHistoryItem]
    total: int
    page: int
    page_size: int


# ---------------------------------------------------------------------------
# 消息查看（CHAT-2）
# ---------------------------------------------------------------------------


class ReferenceCard(CamelModel):
    """引用卡片，附加在 AI 回复消息中的结构化上下文数据。"""

    type: str  # 'customer' | 'record'
    title: str
    summary: str
    data: dict[str, str]  # 键值对详情


class ChatMessageItem(CamelModel):
    """对话消息项。"""

    id: int
    role: str  # 'user' | 'assistant'
    content: str
    created_at: str  # ISO 8601（统一字段名）
    reference_card: ReferenceCard | None = None


class ChatMessagesResponse(CamelModel):
    """CHAT-2 对话消息列表响应。"""

    chat_id: int
    items: list[ChatMessageItem]
    total: int
    page: int
    page_size: int


# ---------------------------------------------------------------------------
# 发送消息（CHAT-3）
# ---------------------------------------------------------------------------


class MessageBrief(CamelModel):
    """消息摘要（用于发送消息响应）。"""

    id: int
    content: str
    created_at: str  # ISO 8601


class SendMessageRequest(CamelModel):
    """CHAT-3 发送消息请求体。"""

    content: str


class SendMessageResponse(CamelModel):
    """CHAT-3 发送消息响应（含用户消息和 AI 回复）。"""

    user_message: MessageBrief
    ai_reply: MessageBrief


# ---------------------------------------------------------------------------
# SSE 流式（CHAT-4）
# ---------------------------------------------------------------------------


class ChatStreamRequest(CamelModel):
    """CHAT-4 SSE 流式请求体。"""

    chat_id: int
    content: str
    source_page: str | None = None
    page_context: dict | None = None
