from pydantic import BaseModel, Field


class ContractTemplateCategoryVO(BaseModel):
    """合同模板分类。"""

    id: int = Field(..., description="分类ID")
    name: str = Field(..., description="分类名称")
    icon: str | None = Field(None, description="分类图标")
    description: str | None = Field(None, description="分类描述")
    sort_order: int = Field(0, description="排序")
    template_count: int = Field(0, description="分类下模板数量")
    is_enabled: bool = Field(True, description="是否启用")


class ContractTemplateListItemVO(BaseModel):
    """合同模板列表项。"""

    id: int = Field(..., description="模板ID")
    template_code: str = Field(..., description="模板编码")
    title: str = Field(..., description="模板标题")
    category_id: int = Field(..., description="分类ID")
    category_name: str | None = Field(None, description="分类名称")
    category_icon: str | None = Field(None, description="分类图标")
    description: str | None = Field(None, description="模板简介")
    region: str = Field(..., description="兼容保留字段：所属租户展示值/旧地区")
    tenant_code: str | None = Field(None, description="租户编码")
    tenant_name: str | None = Field(None, description="租户名称")
    file_path: str | None = Field(None, description="原始模板文件路径")
    pdf_file_path: str | None = Field(None, description="PDF 预览文件路径")
    file_format: str = Field(..., description="文件格式")
    original_file_name: str | None = Field(None, description="原始上传文件名")
    mime_type: str | None = Field(None, description="MIME 类型")
    file_size: int = Field(0, description="文件大小")
    pdf_file_size: int | None = Field(None, description="预览 PDF 文件大小")
    is_featured: bool = Field(False, description="是否推荐")
    created_by: int | None = Field(None, description="创建人")
    updated_by: int | None = Field(None, description="更新人")
    created_at: str | None = Field(None, description="创建时间")
    updated_at: str | None = Field(None, description="更新时间")


class ContractTemplatePageVO(BaseModel):
    """合同模板分页结果。"""

    total: int = Field(..., description="总数")
    page: int = Field(..., description="当前页")
    page_size: int = Field(..., description="分页大小")
    total_pages: int = Field(..., description="总页数")
    templates: list[ContractTemplateListItemVO] = Field(default_factory=list, description="模板列表")


class ContractTemplateDetailVO(ContractTemplateListItemVO):
    """合同模板详情。"""

    category_description: str | None = Field(None, description="分类描述")
    placeholder_schema: dict | None = Field(None, description="模板占位符结构")


class ContractTemplateCreateVO(ContractTemplateDetailVO):
    """合同模板上传结果。"""


class ContractTemplateSearchCategoryVO(BaseModel):
    """搜索结果分类统计。"""

    id: int = Field(..., description="分类ID")
    name: str = Field(..., description="分类名称")
    search_count: int = Field(0, description="当前关键词命中的模板数")


class ContractTemplateSearchResultVO(BaseModel):
    """合同模板搜索结果。"""

    total: int = Field(..., description="总数")
    page: int = Field(..., description="当前页")
    page_size: int = Field(..., description="分页大小")
    total_pages: int = Field(..., description="总页数")
    templates: list[ContractTemplateListItemVO] = Field(default_factory=list, description="模板列表")
    category_stats: list[ContractTemplateSearchCategoryVO] = Field(default_factory=list, description="分类统计")
