"""LeAudit AuditRun 模型 —— leaudit_audit_runs 表。"""

from __future__ import annotations

from datetime import datetime

from sqlalchemy import BigInteger, Boolean, DateTime, Integer, Numeric, String
from sqlalchemy.orm import Mapped, mapped_column

from fastapi_common.fastapi_common_web.models import BaseModel


class LeauditAuditRun(BaseModel):
    """评查运行主表。"""

    __tablename__ = "leaudit_audit_runs"

    Id: Mapped[int] = mapped_column("id", BigInteger, primary_key=True, autoincrement=True)
    documentId: Mapped[int] = mapped_column("document_id", BigInteger, comment="关联 leaudit_documents.id")
    documentFileId: Mapped[int | None] = mapped_column("document_file_id", BigInteger, comment="输入文件ID")
    runNo: Mapped[int] = mapped_column("run_no", Integer, comment="同一文档第几次执行")
    triggerSource: Mapped[str] = mapped_column("trigger_source", String(64), comment="upload/manual/retry/migration/batch")
    triggerUserId: Mapped[int | None] = mapped_column("trigger_user_id", BigInteger, comment="触发人")
    taskId: Mapped[str | None] = mapped_column("task_id", String(128), comment="Celery 任务 ID")

    # 状态
    status: Mapped[str] = mapped_column("status", String(64), default="pending", comment="pending/processing/completed/failed/cancelled")
    phase: Mapped[str | None] = mapped_column("phase", String(32), comment="draft/executed")

    # 规则溯源
    ruleSetId: Mapped[int] = mapped_column("rule_set_id", BigInteger, comment="关联 leaudit_rule_sets.id")
    ruleVersionId: Mapped[int] = mapped_column("rule_version_id", BigInteger, comment="关联 leaudit_rule_versions.id")
    ruleTypeId: Mapped[str | None] = mapped_column("rule_type_id", String(256), comment="LeAudit metadata.type_id")
    ruleSourceOssUrl: Mapped[str | None] = mapped_column("rule_source_oss_url", String(2048), comment="规则 YAML OSS 地址")
    ruleSourceSha256: Mapped[str | None] = mapped_column("rule_source_sha256", String(64), comment="规则文件 SHA256")
    ruleLocalCachePath: Mapped[str | None] = mapped_column("rule_local_cache_path", String(1024), comment="本地缓存路径")

    # 模型快照
    engineVersion: Mapped[str | None] = mapped_column("engine_version", String(64))
    llmProvider: Mapped[str | None] = mapped_column("llm_provider", String(64))
    llmModel: Mapped[str | None] = mapped_column("llm_model", String(128))
    vlmProvider: Mapped[str | None] = mapped_column("vlm_provider", String(64))
    vlmModel: Mapped[str | None] = mapped_column("vlm_model", String(128))
    ocrProvider: Mapped[str | None] = mapped_column("ocr_provider", String(64))
    ocrModel: Mapped[str | None] = mapped_column("ocr_model", String(128))

    # Rescue
    rescueMode: Mapped[str | None] = mapped_column("rescue_mode", String(32), comment="off/tier1/auto")
    rescueApplied: Mapped[bool] = mapped_column("rescue_applied", Boolean, default=False, comment="是否执行 rescue")

    # 结果汇总
    totalScore: Mapped[float | None] = mapped_column("total_score", Numeric(10, 2))
    passedCount: Mapped[int | None] = mapped_column("passed_count", Integer)
    failedCount: Mapped[int | None] = mapped_column("failed_count", Integer)
    skippedCount: Mapped[int | None] = mapped_column("skipped_count", Integer)
    resultStatus: Mapped[str | None] = mapped_column("result_status", String(32), comment="pass/fail/partial/error")

    # 时间
    startedAt: Mapped[datetime | None] = mapped_column("started_at", DateTime(timezone=True))
    finishedAt: Mapped[datetime | None] = mapped_column("finished_at", DateTime(timezone=True))
