"""聊天模型."""

from typing import Literal

from pydantic import BaseModel, Field

from .action_state import ActionState


class ChatMessage(BaseModel):
    """聊天消息."""

    role: Literal["system", "user", "assistant"]
    content: str = Field(..., min_length=1, max_length=8000)


class ChatRequest(BaseModel):
    """聊天请求."""

    messages: list[ChatMessage] = Field(..., min_length=1, max_length=50)
    model: str = "gpt-4o-mini"
    temperature: float = Field(0.7, ge=0.0, le=2.0)
    stream: bool = False
    # 可选上下文（BFF 透传）
    user_id: str | None = None
    session_id: str | None = None
    data_scope: str | None = None


class Usage(BaseModel):
    """Token 用量."""

    prompt_tokens: int = 0
    completion_tokens: int = 0
    total_tokens: int = 0
    latency_ms: int = 0


class ChatData(BaseModel):
    """聊天响应数据（含降级标记）."""

    content: str
    model: str
    usage: Usage
    degraded: bool = False
    degraded_reason: str = ""


class ChatResponse(ActionState[ChatData]):
    """聊天响应信封."""
