# tests/test_randomization_implementation_finder.py
"""
Complete test suite for randomization_implementation_finder.py.
Covers five variants (v1–v5):
    • v1 – high recall (implementation cue)
    • v2 – role + action verb within window
    • v3 – inside Implementation/Assignment heading block
    • v4 – v2 + ≥2 distinct roles nearby
    • v5 – tight template
"""
import pytest
from pyregularexpression.randomization_implementation_finder import (
    find_randomization_implementation_v1,
    find_randomization_implementation_v2,
    find_randomization_implementation_v3,
    find_randomization_implementation_v4,
    find_randomization_implementation_v5,
)

# ────────────────────────────────────
# Robust Tests for v1 (High Recall)
# ────────────────────────────────────
@pytest.mark.parametrize(
    "text, should_match, test_id",
    [
        ("Sequence generated by independent statistician.", True, "v1_pos_statistician_generated"),
        ("Central system assigned participants to groups.", True, "v1_pos_central_system_assigned"),
        ("Investigators enrolled patients.", True, "v1_pos_investigator_enrolled"),
        ("The treatment protocol was implemented by clinicians.", False, "v1_neg_protocol_trap"),
    ],
)
def test_find_randomization_implementation_v1(text, should_match, test_id):
    matches = find_randomization_implementation_v1(text)
    assert bool(matches) == should_match, f"v1 failed for {test_id}"


# ────────────────────────────────────
# Robust Tests for v2 (Role + Action)
# ────────────────────────────────────
@pytest.mark.parametrize(
    "text, should_match, test_id",
    [
        ("The statistician generated the sequence.", True, "v2_pos_statistician_generated"),
        ("Pharmacist enrolled participants into the trial.", True, "v2_pos_pharmacist_enrolled"),
        ("Independent system assigned groups centrally.", True, "v2_pos_system_assigned"),
        ("Statisticians prepared analysis but did not enroll.", False, "v2_neg_wrong_action"),
    ],
)
def test_find_randomization_implementation_v2(text, should_match, test_id):
    matches = find_randomization_implementation_v2(text, window=4)
    assert bool(matches) == should_match, f"v2 failed for {test_id}"


# ────────────────────────────────────
# Lighter Tests for v3 (Heading Block)
# ────────────────────────────────────
@pytest.mark.parametrize(
    "text, should_match, test_id",
    [
        ("Randomisation Implementation:\nSequence generated by a statistician.", True, "v3_pos_heading_block"),
        ("Assignment:\nInvestigators enrolled participants.", True, "v3_pos_assignment_block"),
        ("Enrollment:\n(No details provided)", False, "v3_neg_empty_block"),
        ("Statistician generated the sequence.\nRandomisation Implementation: Not specified.", False, "v3_neg_outside_block"),
    ],
)
def test_find_randomization_implementation_v3(text, should_match, test_id):
    matches = find_randomization_implementation_v3(text)
    assert bool(matches) == should_match, f"v3 failed for {test_id}"


# ────────────────────────────────────
# Lighter Tests for v4 (≥2 Roles)
# ────────────────────────────────────
@pytest.mark.parametrize(
    "text, should_match, test_id",
    [
        ("Statistician generated the sequence; investigators enrolled participants.", True, "v4_pos_two_roles"),
        ("Pharmacist created the list and independent system assigned groups.", True, "v4_pos_pharmacist_system"),
        ("Statistician generated the sequence.", False, "v4_neg_single_role"),
        ("System assigned interventions, but no other role mentioned.", False, "v4_neg_only_system"),
    ],
)
def test_find_randomization_implementation_v4(text, should_match, test_id):
    matches = find_randomization_implementation_v4(text, window=8)
    assert bool(matches) == should_match, f"v4 failed for {test_id}"


# ────────────────────────────────────
# Lighter Tests for v5 (Tight Template)
# ────────────────────────────────────
@pytest.mark.parametrize(
    "text, should_match, test_id",
    [
        ("Statistician generated sequence; investigators enrolled participants; central web system assigned groups.", True, "v5_pos_tight_template"),
        ("Statistician generated the allocation; nurses enrolled participants; IWRS assigned groups.", True, "v5_pos_iwrs_variant"),
        ("Sequence was generated by a statistician and participants enrolled.", False, "v5_neg_incomplete_template"),
        ("System assigned participants centrally (no roles before).", False, "v5_neg_loose_phrase"),
    ],
)
def test_find_randomization_implementation_v5(text, should_match, test_id):
    matches = find_randomization_implementation_v5(text)
    assert bool(matches) == should_match, f"v5 failed for {test_id}"
