Source code for cadrumo.domain.calculations.registry.legal

"""Runtime legal-reference eligibility checks.

The authority artifact carries immutable, already-published legal evidence.
Corpus extraction, textual-grounding and provenance verification are publisher
responsibilities completed before the artifact is published; runtime only
checks whether the artifact's selected citation is eligible for filing.
"""

from __future__ import annotations

from datetime import date

from ....core.revision_review import REVIEWED_REVISION_REVIEW_STATUSES
from .citation_blocklist import find_known_bad
from .errors import RegistryValidationError
from .schema_references import LegalReference






def _validate_known_bad_citation(reference: LegalReference, *, filing_date: date | None = None) -> None:
    if reference.article is None:
        return
    from .setup_profile_bindings import legal_source_kind_declarations

    source = legal_source_kind_declarations().get(str(reference.kind))
    if source is None:
        return
    role_text = " ".join(part for part in (reference.section, reference.notes) if part)
    if role_text and (
        known_bad := find_known_bad(
            source,
            reference.article,
            role_text,
            effective_date=filing_date or reference.effective_from,
            effective_to=None if filing_date is not None else reference.effective_to,
        )
    ):
        raise RegistryValidationError(
            f"legal reference {reference.id!r} matches known-bad citation: {known_bad.reason}",
        )