"""规则管理控制器。"""

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.ruleBindingDto import (
    RuleBindingCreateDTO,
    RuleBindingUpdateDTO,
)
from fastapi_modules.fastapi_leaudit.domian.Dto.rulePublishDto import RulePublishDTO
from fastapi_modules.fastapi_leaudit.domian.Dto.ruleValidateDto import RuleValidateDTO
from fastapi_modules.fastapi_leaudit.domian.Dto.ruleVersionCreateDto import RuleVersionCreateDTO
from fastapi_modules.fastapi_leaudit.domian.vo.ruleVo import (
    RuleBindingVO,
    RuleContentVO,
    RuleSetVO,
    RuleValidationVO,
    RuleVersionVO,
)
from fastapi_modules.fastapi_leaudit.services import IRuleService
from fastapi_modules.fastapi_leaudit.services.impl.ruleServiceImpl import GetRuleServiceSingleton


class RuleController(BaseController):
    """规则管理控制器。"""

    def __init__(self):
        super().__init__(prefix="/rule-sets", tags=["规则管理"])
        self.RuleService: IRuleService = GetRuleServiceSingleton()

        @self.router.get("", response_model=Result[list[RuleSetVO]])
        async def ListRuleSets():
            """列出规则集。"""
            Data = await self.RuleService.ListSets()
            return Result.success(data=Data)

        @self.router.get("/{RuleType}/versions", response_model=Result[list[RuleVersionVO]])
        async def GetVersions(RuleType: str):
            """列出规则集的所有版本。"""
            Data = await self.RuleService.GetVersions(RuleType=RuleType)
            return Result.success(data=Data)

        @self.router.get("/versions/{VersionId}/content", response_model=Result[RuleContentVO])
        async def GetVersionContent(VersionId: int):
            """获取规则版本正文。"""
            Data = await self.RuleService.GetContent(VersionId=VersionId)
            return Result.success(data=Data)

        @self.router.post("/{RuleType}/validate", response_model=Result[RuleValidationVO])
        async def ValidateRuleYaml(RuleType: str, body: RuleValidateDTO):
            """校验规则 YAML。"""
            Data = await self.RuleService.Validate(
                RuleType=RuleType,
                YamlText=body.yamlText,
            )
            return Result.success(data=Data)

        @self.router.post("/{RuleType}/versions", response_model=Result[RuleVersionVO])
        async def CreateRuleVersion(RuleType: str, body: RuleVersionCreateDTO):
            """创建规则版本。"""
            Data = await self.RuleService.CreateVersion(
                RuleType=RuleType,
                YamlText=body.yamlText,
                ChangeNote=body.changeNote,
                EditorUserId=body.editorUserId,
            )
            return Result.success(data=Data)

        @self.router.post("/{RuleType}/publish", response_model=Result[RuleVersionVO])
        async def PublishRuleVersion(RuleType: str, body: RulePublishDTO):
            """发布规则版本。"""
            Data = await self.RuleService.Publish(
                RuleType=RuleType,
                VersionId=body.versionId,
                OperatorUserId=body.operatorUserId,
            )
            return Result.success(data=Data)

        @self.router.post("/{RuleType}/rollback", response_model=Result[RuleVersionVO])
        async def RollbackRuleVersion(RuleType: str, body: RulePublishDTO):
            """回滚到指定规则版本。"""
            Data = await self.RuleService.Rollback(
                RuleType=RuleType,
                VersionId=body.versionId,
                OperatorUserId=body.operatorUserId,
            )
            return Result.success(data=Data)

        # ── 规则类型绑定 ──────────────────────────────────────────

        @self.router.get("/bindings", response_model=Result[list[RuleBindingVO]])
        async def ListBindings(ruleType: str | None = None, region: str | None = None):
            """列出规则类型绑定。可按规则类型/地区过滤。"""
            Data = await self.RuleService.ListBindings(RuleType=ruleType, Region=region)
            return Result.success(data=Data)

        @self.router.post("/{RuleType}/bindings", response_model=Result[RuleBindingVO])
        async def CreateBinding(RuleType: str, body: RuleBindingCreateDTO):
            """创建规则类型绑定。"""
            Data = await self.RuleService.CreateBinding(
                DocTypeId=body.docTypeId,
                RuleSetId=body.ruleSetId,
                Region=body.region,
                BindingMode=body.bindingMode,
                Priority=body.priority,
                DocTypeCode=body.docTypeCode,
                Note=body.note,
            )
            return Result.success(data=Data)

        @self.router.put("/bindings/{BindingId}", response_model=Result[RuleBindingVO])
        async def UpdateBinding(BindingId: int, body: RuleBindingUpdateDTO):
            """更新规则类型绑定。"""
            Data = await self.RuleService.UpdateBinding(
                BindingId=BindingId,
                IsActive=body.isActive,
                Priority=body.priority,
                BindingMode=body.bindingMode,
                Note=body.note,
            )
            return Result.success(data=Data)

        @self.router.delete("/bindings/{BindingId}", response_model=Result[None])
        async def DeleteBinding(BindingId: int):
            """删除规则类型绑定。"""
            await self.RuleService.DeleteBinding(BindingId=BindingId)
            return Result.success()
