from __future__ import annotations

from typing import Any

from pydantic import BaseModel, ConfigDict, Field


class KnowledgeFolderRead(BaseModel):
    name: str
    count: int
    icon: str = "mdi mdi-folder"


class KnowledgePreviewStatRead(BaseModel):
    label: str
    value: str


class KnowledgePreviewBlockRead(BaseModel):
    heading: str
    lines: list[str] = Field(default_factory=list)


class KnowledgePreviewPageRead(BaseModel):
    title: str
    subtitle: str
    stats: list[KnowledgePreviewStatRead] = Field(default_factory=list)
    blocks: list[KnowledgePreviewBlockRead] = Field(default_factory=list)


class KnowledgeDocumentRead(BaseModel):
    id: str
    name: str
    folder: str
    tag: str
    time: str
    version: str
    state: str
    stateTone: str
    owner: str
    icon: str
    fileType: str
    fileTypeLabel: str
    summary: str
    mimeType: str
    extension: str
    sizeBytes: int
    canPreview: bool = False


class KnowledgeDocumentDetailRead(KnowledgeDocumentRead):
    previewKind: str
    previewPages: list[KnowledgePreviewPageRead] = Field(default_factory=list)


class KnowledgeOnlyOfficeConfigRead(BaseModel):
    documentServerUrl: str
    config: dict[str, Any] = Field(default_factory=dict)


class KnowledgeOnlyOfficeCallbackRead(BaseModel):
    error: int = 0


class KnowledgeOnlyOfficeCallbackWrite(BaseModel):
    model_config = ConfigDict(extra="allow")

    status: int = Field(description="ONLYOFFICE 回调状态码。")
    url: str | None = Field(default=None, description="文档下载地址，状态为 2 或 6 时使用。")
    users: list[str] = Field(default_factory=list, description="当前编辑用户列表。")


class KnowledgeLibraryRead(BaseModel):
    folders: list[KnowledgeFolderRead] = Field(default_factory=list)
    documents: list[KnowledgeDocumentRead] = Field(default_factory=list)


class KnowledgeActionResponse(BaseModel):
    ok: bool = True
    detail: str
