"""评查控制器。"""

from typing import Any

from fastapi import Depends

from fastapi_common.fastapi_common_security.security import verify_access_token
from fastapi_common.fastapi_common_web.controller import BaseController
from fastapi_common.fastapi_common_web.domain.responses import Result

from fastapi_modules.fastapi_leaudit.domian.Dto.auditDto import AuditRunDTO
from fastapi_modules.fastapi_leaudit.domian.vo.auditVo import AuditRunVO, AuditResultVO
from fastapi_modules.fastapi_leaudit.services import IAuditService
from fastapi_modules.fastapi_leaudit.services.impl.auditServiceImpl import AuditServiceImpl


class AuditController(BaseController):
    """评查控制器。"""

    def __init__(self):
        super().__init__(prefix="/audit", tags=["评查"])
        self.AuditService: IAuditService = AuditServiceImpl()

        @self.router.post("/run", response_model=Result[AuditRunVO])
        async def RunAudit(body: AuditRunDTO, payload: dict[str, Any] = Depends(verify_access_token)):
            """触发文档评查

            对指定文档执行 LeAudit 完整评查链路。
            """
            run = await self.AuditService.Run(
                DocumentId=body.documentId,
                RuleType=body.ruleType,
                Force=body.force,
                Speed=body.speed,
                TriggerUserId=int(payload["user_id"]),
            )
            return Result.success(data=run)

        @self.router.get("/run/{RunId}", response_model=Result[AuditRunVO])
        async def GetRunStatus(RunId: int):
            """查询评查运行状态。"""
            run = await self.AuditService.GetRunStatus(RunId)
            return Result.success(data=run)

        @self.router.get("/result/{RunId}", response_model=Result[AuditResultVO])
        async def GetResult(RunId: int):
            """获取评查结果。"""
            result = await self.AuditService.GetResult(RunId)
            return Result.success(data=result)
