"""文档 VO。"""

from pydantic import BaseModel, Field

from fastapi_modules.fastapi_leaudit.domian.vo.auditVo import AuditRunVO


class DocumentUploadVO(BaseModel):
    """文档上传响应。"""

    documentId: int = Field(..., description="LeAudit 文档ID")
    internalDocumentNo: int = Field(..., description="平台内部追踪号（兼容旧字段）")
    versionGroupKey: str = Field(..., description="文档版本归档组键")
    versionNo: int = Field(..., description="当前文档版本号")
    previousVersionId: int | None = Field(None, description="上一版本文档ID")
    rootVersionId: int = Field(..., description="文档版本链根文档ID")
    duplicateUpload: bool = Field(..., description="是否命中同名同内容的重复上传")
    fileId: int = Field(..., description="文档文件ID")
    typeId: int = Field(..., description="文档类型ID")
    typeCode: str = Field(..., description="文档类型编码")
    groupId: int | None = Field(None, description="命中的二级分组ID")
    region: str = Field(..., description="所属地区")
    fileName: str = Field(..., description="文件名")
    ossUrl: str = Field(..., description="OSS 对象路径")
    speed: str = Field(..., description="执行速度档位：urgent/normal")
    processingStatus: str = Field(..., description="文档处理状态")
    autoRunTriggered: bool = Field(..., description="是否已自动触发评查")
    run: AuditRunVO | None = Field(None, description="自动触发后的运行信息")


class DocumentStatusItemVO(BaseModel):
    """文档状态项。"""

    documentId: int = Field(..., description="文档ID")
    processingStatus: str | None = Field(None, description="处理状态")
    runStatus: str | None = Field(None, description="当前运行状态")
    phase: str | None = Field(None, description="当前运行阶段")
    resultStatus: str | None = Field(None, description="当前结果状态")
    updatedAt: str | None = Field(None, description="更新时间")


class DocumentHistoryVersionVO(BaseModel):
    """历史版本摘要。"""

    documentId: int = Field(..., description="文档ID")
    fileId: int | None = Field(None, description="文件ID")
    versionNo: int = Field(..., description="版本号")
    fileName: str | None = Field(None, description="文件名")
    fileExt: str | None = Field(None, description="文件扩展名")
    processingStatus: str | None = Field(None, description="处理状态")
    runStatus: str | None = Field(None, description="最新运行状态")
    resultStatus: str | None = Field(None, description="最新结果状态")
    updatedAt: str | None = Field(None, description="更新时间")


class DocumentAttachmentVO(BaseModel):
    """文档附件项。"""

    fileId: int = Field(..., description="附件文件ID")
    fileName: str = Field(..., description="附件文件名")
    fileExt: str | None = Field(None, description="附件扩展名")
    mimeType: str | None = Field(None, description="附件 MIME 类型")
    fileSize: int | None = Field(None, description="附件文件大小")
    fileRole: str = Field(..., description="文件角色")
    ossUrl: str | None = Field(None, description="附件 OSS 路径")
    createdBy: int | None = Field(None, description="上传人")
    createdAt: str | None = Field(None, description="上传时间")


class DocumentListItemVO(BaseModel):
    """文档列表项。"""

    documentId: int = Field(..., description="文档ID")
    internalDocumentNo: int = Field(..., description="平台内部追踪号")
    versionGroupKey: str = Field(..., description="版本归档组键")
    versionNo: int = Field(..., description="当前版本号")
    rootVersionId: int = Field(..., description="根版本文档ID")
    previousVersionId: int | None = Field(None, description="上一版本文档ID")
    typeId: int | None = Field(None, description="文档类型ID")
    typeCode: str | None = Field(None, description="文档类型编码")
    typeName: str | None = Field(None, description="文档类型名称")
    groupId: int | None = Field(None, description="命中的二级分组ID")
    groupName: str | None = Field(None, description="二级分组名称")
    region: str = Field(..., description="区域")
    normalizedName: str | None = Field(None, description="归一化名称")
    fileId: int | None = Field(None, description="文件ID")
    fileName: str | None = Field(None, description="文件名")
    fileExt: str | None = Field(None, description="文件扩展名")
    mimeType: str | None = Field(None, description="MIME类型")
    fileSize: int | None = Field(None, description="文件大小")
    ossUrl: str | None = Field(None, description="OSS路径")
    processingStatus: str | None = Field(None, description="处理状态")
    currentRunId: int | None = Field(None, description="当前运行ID")
    runStatus: str | None = Field(None, description="当前运行状态")
    resultStatus: str | None = Field(None, description="当前结果状态")
    latestErrorCode: str | None = Field(None, description="最新失败错误码")
    latestErrorMessage: str | None = Field(None, description="最新失败错误信息")
    totalScore: float | None = Field(None, description="总分")
    passedCount: int | None = Field(None, description="通过数")
    failedCount: int | None = Field(None, description="失败数")
    skippedCount: int | None = Field(None, description="跳过数")
    documentNumber: str | None = Field(None, description="业务文号/案号")
    auditStatus: int | None = Field(None, description="人工维护审核状态")
    isTestDocument: bool = Field(False, description="是否测试文档")
    updatedAt: str | None = Field(None, description="更新时间")
    hasHistory: bool = Field(False, description="是否存在历史版本")
    totalVersions: int = Field(1, description="总版本数")
    historyVersions: list[DocumentHistoryVersionVO] = Field(default_factory=list, description="历史版本摘要")


class DocumentDetailVO(DocumentListItemVO):
    """文档详情。"""

    remark: str | None = Field(None, description="备注")
    pageCount: int | None = Field(None, description="页数，暂无精确值时可为空")
    attachments: list[DocumentAttachmentVO] = Field(default_factory=list, description="附件列表")


class DocumentTypeItemVO(BaseModel):
    """文档类型列表项。"""

    id: int = Field(..., description="类型ID")
    name: str = Field(..., description="类型名称")
    code: str = Field(..., description="类型编码")
    description: str | None = Field(None, description="描述")
    entryModuleId: int | None = Field(None, description="入口模块ID")
    isEnabled: bool = Field(True, description="是否启用")
    ruleSetIds: list[int] = Field(default_factory=list, description="关联的规则集ID")


class DocumentTypeRootItemVO(BaseModel):
    """一级文档类型（业务大类）列表项。"""

    id: int = Field(..., description="一级分组ID")
    name: str = Field(..., description="一级分组名称")
    code: str = Field(..., description="一级分组编码")
    description: str | None = Field(None, description="描述")
    entryModuleId: int | None = Field(None, description="入口模块ID")
    entryModuleName: str | None = Field(None, description="入口模块名称")
    isEnabled: bool = Field(True, description="是否启用")
    childGroupCount: int = Field(0, description="下属二级分组数量")
    ruleSetCount: int = Field(0, description="汇总规则集数量")
    ruleSetIds: list[int] = Field(default_factory=list, description="汇总规则集ID")


class DocumentTypeRootCreateDTO(BaseModel):
    """创建一级文档类型（业务大类）。"""

    code: str = Field(..., description="一级分组编码")
    name: str = Field(..., description="一级分组名称")
    description: str = Field("", description="描述")
    entryModuleId: int | None = Field(None, description="入口模块ID")
    isEnabled: bool = Field(True, description="是否启用")
    sortOrder: int = Field(0, description="排序")


class DocumentTypeRootUpdateDTO(BaseModel):
    """更新一级文档类型（业务大类）。"""

    name: str | None = Field(None, description="一级分组名称")
    description: str | None = Field(None, description="描述")
    entryModuleId: int | None = Field(None, description="入口模块ID")
    isEnabled: bool | None = Field(None, description="是否启用")
    sortOrder: int | None = Field(None, description="排序")


class DocumentTypeCreateDTO(BaseModel):
    """文档类型创建请求。"""

    code: str = Field(..., description="类型编码")
    name: str = Field(..., description="类型名称")
    description: str = Field("", description="描述")
    entryModuleId: int | None = Field(None, description="入口模块ID")
    isEnabled: bool = Field(True, description="是否启用")
    sortOrder: int = Field(0, description="排序")
    ruleSetIds: list[int] = Field(default_factory=list, description="关联的规则集ID")


class DocumentTypeUpdateDTO(BaseModel):
    """文档类型更新请求。"""

    name: str | None = Field(None, description="类型名称")
    description: str | None = Field(None, description="描述")
    entryModuleId: int | None = Field(None, description="入口模块ID")
    isEnabled: bool | None = Field(None, description="是否启用")
    sortOrder: int | None = Field(None, description="排序")
    ruleSetIds: list[int] | None = Field(None, description="关联的规则集ID（传则全量替换）")


class DocumentListPageVO(BaseModel):
    """文档列表分页结果。"""

    total: int = Field(..., description="总数")
    page: int = Field(..., description="当前页")
    pageSize: int = Field(..., description="每页数量")
    totalPages: int = Field(..., description="总页数")
    documents: list[DocumentListItemVO] = Field(default_factory=list, description="文档列表")


class DocumentUpdateDTO(BaseModel):
    """文档元数据更新请求。"""

    documentNumber: str | None = Field(None, description="业务文号/案号")
    auditStatus: int | None = Field(None, description="人工维护审核状态（若表结构支持则写入）")
    isTestDocument: bool | None = Field(None, description="是否测试文档")
    remark: str | None = Field(None, description="备注")
