from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field

from kbqa.documents.schemas import Page


class SessionCreate(BaseModel):
    title: str | None = Field(default=None, max_length=200)


class ChatStreamRequest(BaseModel):
    session_id: str
    query: str = Field(min_length=1, max_length=10_000)


class SessionResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    title: str | None
    created_at: datetime
    updated_at: datetime


class MessageSourceResponse(BaseModel):
    label: str
    document_id: str
    chunk_id: str
    filename: str
    order_index: int
    page_number: int | None
    content: str
    score: float


class MessageResponse(BaseModel):
    id: str
    session_id: str
    role: str
    content: str
    status: str
    error_code: str | None
    order_index: int
    created_at: datetime
    sources: list[MessageSourceResponse] = Field(default_factory=list)
    deleted_source_labels: list[str] = Field(default_factory=list)


SessionPage = Page[SessionResponse]
MessagePage = Page[MessageResponse]
