"""评查 VO。"""

from datetime import datetime

from pydantic import BaseModel, Field


class AuditRunVO(BaseModel):
    """评查运行响应。"""

    runId: int = Field(..., description="运行ID")
    documentId: int = Field(..., description="文档ID")
    runNo: int = Field(..., description="执行序号")
    documentFileId: int | None = Field(None, description="本次执行锁定的文档文件ID")
    status: str = Field(..., description="状态")
    phase: str | None = Field(None, description="当前阶段")
    resultStatus: str | None = Field(None, description="整体结果: pass/fail/partial/review/error")
    ruleSetId: int | None = Field(None, description="规则集ID")
    ruleVersionId: int | None = Field(None, description="规则版本ID")
    ruleTypeId: str | None = Field(None, description="规则类型ID")
    rescueApplied: bool = Field(False, description="是否触发 rescue")
    totalScore: float | None = Field(None, description="总分")
    passedCount: int | None = Field(None, description="通过数")
    failedCount: int | None = Field(None, description="失败数")
    skippedCount: int | None = Field(None, description="跳过数")
    startedAt: datetime | None = Field(None, description="开始时间")
    finishedAt: datetime | None = Field(None, description="结束时间")


class AuditFieldResultVO(BaseModel):
    """抽取字段结果。"""

    fieldName: str = Field(..., description="字段名")
    fieldType: str | None = Field(None, description="字段类型")
    valueText: str | None = Field(None, description="字段值")
    confidence: float | None = Field(None, description="综合置信度")
    groundingMethod: str | None = Field(None, description="落地验证方法")
    fallbackValue: str | None = Field(None, description="兜底值")
    rawValueJson: dict | list | str | None = Field(None, description="原始抽取结果")
    metaJson: dict | list | str | None = Field(None, description="扩展元数据")


class AuditRunErrorVO(BaseModel):
    """运行错误结果。"""

    stage: str = Field(..., description="错误阶段")
    level: str = Field(..., description="错误级别")
    errorCode: str | None = Field(None, description="错误码")
    message: str = Field(..., description="错误信息")
    detailJson: dict | list | str | None = Field(None, description="错误详情")
    createdAt: datetime | None = Field(None, description="创建时间")


class AuditRescueOutcomeVO(BaseModel):
    """补救结果。"""

    ruleId: str = Field(..., description="规则ID")
    status: str = Field(..., description="补救状态")
    diagnosis: str | None = Field(None, description="诊断结果")
    diagnosisConfidence: float | None = Field(None, description="诊断置信度")
    finalStatus: str | None = Field(None, description="补救最终状态")
    failureReason: str | None = Field(None, description="失败原因")
    llmCalls: int | None = Field(None, description="LLM 调用次数")
    vlmCalls: int | None = Field(None, description="VLM 调用次数")
    durationMs: int | None = Field(None, description="耗时毫秒")
    requiresHumanReview: bool = Field(False, description="是否需要人工复核")
    payload: dict | list | str | None = Field(None, description="补救载荷")


class AuditMetricsVO(BaseModel):
    """运行指标。"""

    ocrSeconds: float | None = Field(None, description="OCR耗时")
    normalizeSeconds: float | None = Field(None, description="归一化耗时")
    extractSeconds: float | None = Field(None, description="抽取耗时")
    evaluateSeconds: float | None = Field(None, description="评查耗时")
    rescueSeconds: float | None = Field(None, description="补救耗时")
    totalSeconds: float | None = Field(None, description="总耗时")
    pageCount: int | None = Field(None, description="页数")
    subDocumentCount: int | None = Field(None, description="子文档数")
    fieldCount: int | None = Field(None, description="字段数")
    ruleCount: int | None = Field(None, description="规则数")
    llmCallCount: int | None = Field(None, description="LLM 调用次数")
    vlmCallCount: int | None = Field(None, description="VLM 调用次数")
    rescueRuleCount: int | None = Field(None, description="补救规则数")
    artifactCount: int | None = Field(None, description="产物数")


class AuditArtifactVO(BaseModel):
    """评查产物。"""

    artifactType: str = Field(..., description="产物类型")
    artifactRole: str | None = Field(None, description="产物角色")
    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 路径")
    isPersisted: bool | None = Field(None, description="是否已持久化")


class AuditResultVO(BaseModel):
    """评查结果响应。"""

    runId: int = Field(..., description="运行ID")
    documentId: int = Field(..., description="文档ID")
    documentFileId: int | None = Field(None, description="本次执行锁定的文档文件ID")
    status: str = Field(..., description="运行状态")
    totalScore: float | None = Field(None, description="总分")
    passedCount: int = Field(0, description="通过数")
    failedCount: int = Field(0, description="失败数")
    skippedCount: int = Field(0, description="跳过数")
    phase: str | None = Field(None, description="当前阶段")
    resultStatus: str | None = Field(None, description="整体结果")
    rescueApplied: bool = Field(False, description="是否执行 rescue")
    ruleSetId: int | None = Field(None, description="规则集ID")
    ruleVersionId: int | None = Field(None, description="规则版本ID")
    startedAt: datetime | None = Field(None, description="开始时间")
    finishedAt: datetime | None = Field(None, description="结束时间")
    rules: list[dict] = Field(default_factory=list, description="规则结果列表")
    fields: list[AuditFieldResultVO] = Field(default_factory=list, description="抽取字段结果")
    errors: list[AuditRunErrorVO] = Field(default_factory=list, description="运行错误列表")
    rescueOutcomes: list[AuditRescueOutcomeVO] = Field(default_factory=list, description="补救结果列表")
    metrics: AuditMetricsVO | None = Field(None, description="运行指标")
    artifacts: list[AuditArtifactVO] = Field(default_factory=list, description="运行产物列表")
