"""required check：目标实体或选中段落必须有非空文本。"""

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("required")
class RequiredCheck(CheckBase):
    def run(self, ctx: CheckContext) -> CheckResult:
        # target 通道：检查实体 text 是否非空
        if ctx.target is not None:
            if ctx.target.text and ctx.target.text.strip():
                return CheckResult(passed=True, hits=[])
            anchor = ctx.paragraphs[0] if ctx.paragraphs else None
            return CheckResult(passed=False, hits=[
                CheckHit(paragraph=anchor, message=f"实体 {ctx.target.name} 缺失或为空")
            ])

        # applies_to 通道：所有段落必须非空
        empty = [p for p in ctx.paragraphs if not p.text.strip()]
        if empty:
            return CheckResult(
                passed=False,
                hits=[CheckHit(paragraph=p, message="段落为空") for p in empty],
            )
        return CheckResult(passed=True, hits=[])
