"""页级图片质量控制器。"""

from typing import Any

from fastapi import Depends, Form

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.vo.pageQualityVo import (
    PageQualityDetailVO,
    PageQualityRecheckVO,
    PageQualitySummaryVO,
)
from fastapi_modules.fastapi_leaudit.services.impl.pageQualityServiceImpl import PageQualityServiceImpl
from fastapi_modules.fastapi_leaudit.services.pageQualityService import IPageQualityService


class PageQualityController(BaseController):
    """页级图片质量控制器。"""

    def __init__(self):
        super().__init__(prefix="", tags=["页级图片质量"])
        self.PageQualityService: IPageQualityService = PageQualityServiceImpl()

        @self.router.get("/documents/{DocumentId}/page-quality/summary", response_model=Result[PageQualitySummaryVO])
        async def GetDocumentPageQualitySummary(
            DocumentId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取文档页级模糊检测摘要。"""
            data = await self.PageQualityService.GetDocumentSummary(
                CurrentUserId=int(payload["user_id"]),
                DocumentId=DocumentId,
            )
            return Result.success(data=data)

        @self.router.get("/documents/{DocumentId}/page-quality", response_model=Result[PageQualityDetailVO])
        async def GetDocumentPageQualityDetail(
            DocumentId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取文档页级模糊检测详情。"""
            data = await self.PageQualityService.GetDocumentDetail(
                CurrentUserId=int(payload["user_id"]),
                DocumentId=DocumentId,
            )
            return Result.success(data=data)

        @self.router.post("/documents/{DocumentId}/page-quality/recheck", response_model=Result[PageQualityRecheckVO])
        async def RecheckDocumentPageQuality(
            DocumentId: int,
            speed: str = Form("normal", description="执行速度档位：urgent/normal"),
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """手工重跑页级模糊检测。"""
            data = await self.PageQualityService.RecheckDocument(
                CurrentUserId=int(payload["user_id"]),
                DocumentId=DocumentId,
                Speed=speed,
            )
            return Result.success(data=data, message="页级模糊检测任务已投递")
