"""短语/字符黑名单。"""

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


@register("forbid_phrase")
class ForbidPhraseCheck(CheckBase):
    def run(self, ctx: CheckContext) -> CheckResult:
        phrases = ctx.stage.phrases or []
        hits: list[CheckHit] = []
        for p in ctx.paragraphs:
            for phr in phrases:
                start = p.text.find(phr)
                if start >= 0:
                    hits.append(CheckHit(
                        paragraph=p, char_start=start, char_end=start + len(phr),
                        actual={"text": phr}, expected={"forbid": phr},
                        message=f"出现禁用短语 \"{phr}\"",
                    ))
        return CheckResult(passed=not hits, hits=hits)


@register("forbid_chars")
class ForbidCharsCheck(CheckBase):
    def run(self, ctx: CheckContext) -> CheckResult:
        chars = ctx.stage.chars or []
        hits: list[CheckHit] = []
        for p in ctx.paragraphs:
            for c in chars:
                start = 0
                while True:
                    idx = p.text.find(c, start)
                    if idx < 0:
                        break
                    hits.append(CheckHit(
                        paragraph=p, char_start=idx, char_end=idx + len(c),
                        actual={"char": c}, expected={"forbid": c},
                        message=f"禁用字符 \"{c}\" 出现在 idx {idx}",
                    ))
                    start = idx + len(c)
        return CheckResult(passed=not hits, hits=hits)
