"""rule_type coercion after the authority/role split (INV-DM7).

The extractor's rule_type holds the rule ROLE only — it is never defaulted from
the source's bindingness. Legacy authority values fold to the nearest role.
"""
from legal_mcp.services.halacha_extractor import (
    _LEGACY_RULE_TYPE_FOLD,
    _VALID_RULE_TYPES,
    _coerce_halacha,
)

_BASE = {"rule_statement": "כלל כלשהו", "supporting_quote": "ציטוט תומך כלשהו"}


def _rt(rule_type):
    return _coerce_halacha({**_BASE, "rule_type": rule_type})["rule_type"]


def test_valid_roles_are_the_five_roles_only():
    assert _VALID_RULE_TYPES == {
        "holding", "interpretive", "procedural", "application", "obiter",
    }
    assert "binding" not in _VALID_RULE_TYPES
    assert "persuasive" not in _VALID_RULE_TYPES


def test_legacy_authority_values_fold_to_a_role():
    assert _rt("binding") == "holding"
    assert _rt("persuasive") == "interpretive"
    assert _LEGACY_RULE_TYPE_FOLD == {"binding": "holding", "persuasive": "interpretive"}


def test_genuine_roles_pass_through():
    for role in ("holding", "interpretive", "procedural", "application", "obiter"):
        assert _rt(role) == role


def test_unknown_or_missing_defaults_to_interpretive():
    assert _rt("nonsense") == "interpretive"
    assert _coerce_halacha(_BASE)["rule_type"] == "interpretive"


def test_coerce_rejects_rows_missing_required_fields():
    assert _coerce_halacha({"rule_statement": "x"}) is None
    assert _coerce_halacha({"supporting_quote": "y"}) is None
    assert _coerce_halacha("not a dict") is None
