"""文种白名单（15 种法定公文文种）。"""

from fastapi_modules.fastapi_leaudit.govdoc_engine.engine.checks import register
from fastapi_modules.fastapi_leaudit.govdoc_engine.engine.checks.base import (
    CheckBase, CheckContext, CheckHit, CheckResult,
)


LEGAL_WENZHONG = {
    "决议", "决定", "命令", "令", "公报", "公告", "通告",
    "意见", "通知", "通报", "报告", "请示", "批复",
    "议案", "函", "纪要",
}


@register("wenzhong_whitelist")
class WenzhongWhitelistCheck(CheckBase):
    """检查文种是否在 15 种法定文种白名单内。

    数据来源：
    1. ctx.entities["wenzhong"].text   ← 推荐
    2. ctx.target.text  （当 rule.target = wenzhong 时）
    """

    def run(self, ctx: CheckContext) -> CheckResult:
        wz = ""
        wz_entity = ctx.entities.get("wenzhong") if ctx.entities else None
        if wz_entity is not None:
            wz = (wz_entity.text or "").strip()
        elif ctx.target is not None and ctx.target.name == "wenzhong":
            wz = (ctx.target.text or "").strip()

        if not wz:
            return CheckResult(passed=True, hits=[])
        if wz in LEGAL_WENZHONG:
            return CheckResult(passed=True, hits=[])
        return CheckResult(passed=False, hits=[CheckHit(
            paragraph=None,
            actual={"wenzhong": wz},
            expected={"wenzhong_whitelist": sorted(LEGAL_WENZHONG)},
            message=f"非法定文种 \"{wz}\"，应为 15 种法定公文文种之一",
        )])
