"""Govdoc 公文模块控制器。

提供公文上传、列表、详情、审查运行、结果与报告、规则查看等接口。
"""

from __future__ import annotations

from typing import Any

from fastapi import Depends, File, Form, Query, UploadFile

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.services import IGovdocService
from fastapi_modules.fastapi_leaudit.services.impl.govdocServiceImpl import GovdocServiceImpl


class GovdocController(BaseController):
    """公文处理与格式审查控制器。"""

    def __init__(self):
        super().__init__(prefix="/govdoc", tags=["内部公文"])
        self.GovdocService: IGovdocService = GovdocServiceImpl()

        # ── 文档 ──────────────────────────────────────────

        @self.router.post("/documents")
        async def UploadDocument(
            file: UploadFile = File(...),
            typeId: int | None = Form(default=None),
            region: str | None = Form(default=None, description="兼容保留字段：租户展示值/旧地区"),
            tenant_code: str | None = Form(default=None, description="租户编码"),
            autoRun: bool = Form(default=True),
            speed: str = Form(default="normal"),
            ruleVersionId: int | None = Form(default=None),
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """上传公文文档。

            创建文档主档记录，engine_type 标记为 govdoc，可选自动触发审查。
            """
            result = await self.GovdocService.UploadDocument(
                file=file,
                typeId=typeId,
                region=region,
                tenantCode=tenant_code,
                autoRun=autoRun,
                speed=speed,
                ruleVersionId=ruleVersionId,
                createdBy=int(payload["user_id"]),
            )
            return Result.success(data=result)

        @self.router.get("/documents")
        async def ListDocuments(
            page: int = Query(default=1, ge=1),
            pageSize: int = Query(default=20, ge=1, le=100),
            keyword: str | None = Query(default=None),
            fileExt: str | None = Query(default=None),
            region: str | None = Query(default=None, description="兼容保留字段：租户展示值/旧地区"),
            tenant_code: str | None = Query(default=None, description="租户编码"),
            status: str | None = Query(default=None),
            resultStatus: str | None = Query(default=None),
            createdBy: int | None = Query(default=None),
            dateFrom: str | None = Query(default=None),
            dateTo: str | None = Query(default=None),
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取公文模块文档列表。

            后端自动附加 engine_type='govdoc' 过滤条件。
            """
            result = await self.GovdocService.ListDocuments(
                page=page,
                pageSize=pageSize,
                keyword=keyword,
                fileExt=fileExt,
                region=region,
                tenantCode=tenant_code,
                status=status,
                resultStatus=resultStatus,
                createdBy=createdBy,
                dateFrom=dateFrom,
                dateTo=dateTo,
                userId=int(payload["user_id"]),
            )
            return Result.success(data=result)

        @self.router.get("/documents/{documentId}")
        async def GetDocumentDetail(
            documentId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取公文详情：文档基础信息 + 最新 run 摘要 + 报告引用。"""
            result = await self.GovdocService.GetDocumentDetail(
                documentId=documentId,
                userId=int(payload["user_id"]),
            )
            return Result.success(data=result)

        @self.router.patch("/documents/{documentId}")
        async def UpdateDocument(
            documentId: int,
            body: dict[str, Any],
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """修改公文标题、文号、备注等基础信息。"""
            result = await self.GovdocService.UpdateDocument(
                documentId=documentId,
                body=body,
                userId=int(payload["user_id"]),
            )
            return Result.success(data=result)

        @self.router.delete("/documents/{documentId}")
        async def DeleteDocument(
            documentId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """软删除公文文档。"""
            result = await self.GovdocService.DeleteDocument(
                documentId=documentId,
                userId=int(payload["user_id"]),
            )
            return Result.success(data=result)

        # ── 审查运行 ──────────────────────────────────────

        @self.router.post("/runs")
        async def CreateRun(
            documentId: int = Form(...),
            ruleVersionId: int | None = Form(default=None),
            speed: str = Form(default="normal"),
            force: bool = Form(default=False),
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """对已存在文档发起一次公文审查 run。

            创建 govdoc_runs 记录，投递 Celery worker。
            """
            result = await self.GovdocService.CreateRun(
                documentId=documentId,
                ruleVersionId=ruleVersionId,
                speed=speed,
                force=force,
                triggerUserId=int(payload["user_id"]),
            )
            return Result.success(data=result)

        @self.router.get("/runs/{runId}")
        async def GetRunStatus(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """查询 run 状态、阶段、耗时、错误摘要。"""
            result = await self.GovdocService.GetRunStatus(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        # ── 结果与报告 ────────────────────────────────────

        @self.router.get("/runs/{runId}/result")
        async def GetRunResult(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取审查结果摘要：summary + checked rules + findings 统计 + entities 摘要。"""
            result = await self.GovdocService.GetRunResult(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/runs/{runId}/findings")
        async def GetRunFindings(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取段落级 findings 明细列表。"""
            result = await self.GovdocService.GetRunFindings(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/runs/{runId}/entities")
        async def GetRunEntities(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取识别出的标题、文号、署名等实体。"""
            result = await self.GovdocService.GetRunEntities(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/runs/{runId}/structure")
        async def GetRunStructure(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取文档结构统计（结构面板数据）。"""
            result = await self.GovdocService.GetRunStructure(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/runs/{runId}/outline")
        async def GetRunOutline(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取文档大纲树（大纲面板数据）。"""
            result = await self.GovdocService.GetRunOutline(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/runs/{runId}/paragraphs")
        async def GetRunParagraphs(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取前端文档联动视图所需的段落 HTML。"""
            result = await self.GovdocService.GetRunParagraphs(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/runs/{runId}/report/html")
        async def GetReportHtml(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取 HTML 报告内容或下载地址。"""
            result = await self.GovdocService.GetReportHtml(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/runs/{runId}/report/docx")
        async def GetReportDocx(
            runId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取批注 DOCX 下载地址。"""
            result = await self.GovdocService.GetReportDocx(runId=runId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        @self.router.get("/documents/{documentId}/original")
        async def DownloadOriginal(
            documentId: int,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取原始上传文档下载地址。"""
            result = await self.GovdocService.DownloadOriginal(documentId=documentId, userId=int(payload["user_id"]))
            return Result.success(data=result)

        # ── 规则 ──────────────────────────────────────────

        @self.router.get("/rules")
        async def ListRules(
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取当前生效规则集摘要。"""
            result = await self.GovdocService.ListRules()
            return Result.success(data=result)

        @self.router.get("/rules/{ruleId}")
        async def GetRuleDetail(
            ruleId: str,
            payload: dict[str, Any] = Depends(verify_access_token),
        ):
            """获取单条规则完整详情。"""
            result = await self.GovdocService.GetRuleDetail(ruleId=ruleId)
            return Result.success(data=result)
