"""#232 trap 3 — a well-formed docket is not necessarily the right docket.

ערר (מרכז) 1094-09-19 (פדילה) was stored as ``1094-09-14``: shape-valid, so
``_is_clean_docket`` waved it through, but wrong — and case_number is the
identity field, so the row detached from every reference to the real case.
Grounding the digits in the source text is what shape validation cannot do.
"""

from legal_mcp.services.precedent_metadata_extractor import (
    _docket_grounded,
    _is_clean_docket,
    _strip_invisibles,
)


HEADER = "ערר (ועדות ערר - תכנון ובנייה מרכז) 1094-09-19 פדילה אברהים נ' הוועדה המקומית"


def test_the_regression_shape_valid_but_wrong_digit():
    """Both pass the shape check; only the real one is grounded."""
    assert _is_clean_docket("1094-09-14")
    assert _is_clean_docket("1094-09-19")
    assert _docket_grounded("1094-09-19", HEADER)
    assert not _docket_grounded("1094-09-14", HEADER)


def test_separator_and_spacing_are_tolerated():
    """A real docket must still match when the source writes it differently."""
    assert _docket_grounded("1094-09-19", "בערר 1094/09/19 נקבע")
    assert _docket_grounded("4768/22", "עת\"מ 4768-22 פלוני")
    assert _docket_grounded("1132-09-24", "תיק 1132 - 09 - 24")


def test_bidi_marks_inside_the_number_do_not_defeat_grounding():
    """Hebrew legal PDFs embed RLM/LRM between digits and separators."""
    noisy = "ערר ‏(‏מרכז‏)‏ 1094‏-‏09‏-‏19 פדילה"
    assert _strip_invisibles(noisy).count("‏") == 0
    assert _docket_grounded("1094-09-19", noisy)


def test_grounding_accepts_the_value_being_replaced():
    """Normalising an uploader's citation string into a clean docket is the
    whole point of the rewrite — the digits come from there, not the text."""
    citation = "ערר (ועדות ערר - תכנון ובנייה מרכז) 1094-09-19 פדילה נ' טירה (נבו 4.12.2019)"
    assert _docket_grounded("1094-09-19", "", citation)
    assert not _docket_grounded("1094-09-14", "", citation)


def test_two_and_three_part_dockets_both_ground():
    assert _docket_grounded("8031/21", "בהיטל השבחה 8031/21 נדון")
    assert _docket_grounded("85074-09-24", "בל\"מ 85074-09-24")


def test_non_numeric_or_empty_never_grounds():
    assert not _docket_grounded("", HEADER)
    assert not _docket_grounded("ערר 1094", HEADER)
    assert not _docket_grounded("abc-de", HEADER)


def test_absent_from_every_source_is_refused():
    """The פדילה failure mode: text has no docket at all, so anything the
    model offers is ungrounded and must not reach the identity field."""
    body = "בפני: יו\"ר הוועדה: רונית אלפר, עו\"ד\nהעוררים: 1. פדילה אברהים"
    assert not _docket_grounded("1094-09-14", body)
    assert not _docket_grounded("1094-09-19", body)
