from app.utils.pattern_suggestion import compute_selection_context, suggest_regex


def test_compute_selection_context_united_india_insured():
    text = (
        "PERIOD OF INSURANCE From 00:00 Hrs of 08/11/2023 To Midnight of 07/11/2024 "
        "Insured MR RAMESH S/O SH DHARMVEER R/O 377/35 JANTA COLONY ROHTAK HR 124001 ROHTAK HARYANA "
        "CONTACT NUMBER: 9812878250"
    )
    start = text.index("MR RAMESH")
    end = start + len("MR RAMESH")
    ctx = compute_selection_context(text, start, end)

    assert ctx["value"] == "MR RAMESH"
    assert "Insured" in ctx["anchor_before"]
    assert ctx["anchor_after"].startswith(" S/O")


def test_suggest_regex_customer_name_with_insured_anchor():
    regex = suggest_regex(
        "customer_name",
        "MR RAMESH",
        "Insured ",
        " S/O SH DHARMVEER",
    )
    assert "Insured" in regex
    assert "MR" in regex

    text = "Insured MR RAMESH S/O SH DHARMVEER R/O 377/35"
    import re

    match = re.search(regex, text, re.IGNORECASE)
    assert match
    assert match.group(1) == "MR RAMESH"
