"""文档控制器。"""

from fastapi import File, Form, UploadFile

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.documentVo import DocumentListPageVO, DocumentUploadVO
from fastapi_modules.fastapi_leaudit.services import IDocumentService
from fastapi_modules.fastapi_leaudit.services.impl.documentServiceImpl import DocumentServiceImpl


class DocumentController(BaseController):
    """文档控制器。"""

    def __init__(self):
        super().__init__(prefix="", tags=["文档"])
        self.DocumentService: IDocumentService = DocumentServiceImpl()

        @self.router.post("/upload", response_model=Result[DocumentUploadVO])
        async def UploadDocument(
            file: UploadFile = File(..., description="上传文档"),
            typeId: int | None = Form(None, description="文档类型ID"),
            typeCode: str | None = Form(None, description="文档类型编码"),
            region: str = Form("default", description="所属地区"),
            fileRole: str = Form("primary", description="文件角色"),
            createdBy: int | None = Form(None, description="上传用户ID"),
            autoRun: bool = Form(False, description="是否上传后自动触发评查"),
            speed: str = Form("normal", description="执行速度档位：urgent/normal"),
        ):
            """上传文档并建立评查输入。"""
            Content = await file.read()
            Data = await self.DocumentService.Upload(
                FileName=file.filename or "upload.bin",
                FileContent=Content,
                ContentType=file.content_type,
                TypeId=typeId,
                TypeCode=typeCode,
                Region=region,
                FileRole=fileRole,
                CreatedBy=createdBy,
                AutoRun=autoRun,
                Speed=speed,
            )
            return Result.success(data=Data)

        @self.router.get("/documents/list", response_model=Result[DocumentListPageVO])
        async def ListDocuments(
            page: int = 1,
            pageSize: int = 20,
            keyword: str | None = None,
            typeCode: str | None = None,
            region: str | None = None,
            processingStatus: str | None = None,
            resultStatus: str | None = None,
        ):
            """获取文档列表（仅返回最新版本，附历史版本摘要）。"""
            Data = await self.DocumentService.ListDocuments(
                Page=page,
                PageSize=pageSize,
                Keyword=keyword,
                TypeCode=typeCode,
                Region=region,
                ProcessingStatus=processingStatus,
                ResultStatus=resultStatus,
            )
            return Result.success(data=Data)
