"""Changing the reason explicitly must clean it the same way the first one was.

Reported with screenshots. "BANK WORK" typed as the first reason came back
"Bank work" -- the extractor cleaned it. But asking for a change did not go
near the extractor:

    "reason should be change bANK wORk"    -> card said "Change bANK wORk"
    "reason should be changed to bANk wOrK" -> card said "Changed to bANk wOrK"

Two faults in one line. The wrapper ("change", "changed to") was captured as
part of the reason, and nothing cleaned the casing, so the same words produced
different results depending only on which turn they arrived on -- which is the
inconsistency HA-666 exists to remove.

The regex now only isolates WHICH words are the reason; the extractor decides
how they read. The regex result survives as the fallback for when the model
is unreachable, and it strips the wrapper too so that fallback is not wrong,
just uncleaned.
"""
import pytest

import services.leave_action_service as las


@pytest.mark.parametrize("query,expected", [
    ("reason should be changed to bank work", "Bank work"),
    ("reason should be updated to family function", "Family function"),
    ("reason should be set to dentist appointment", "Dentist appointment"),
    ("reason should be corrected to bank work", "Bank work"),
])
def test_the_fallback_strips_the_change_wrapper(query, expected):
    """Without this the wrapper went on the card verbatim. Casing is the
    extractor's job; this only has to isolate the right words."""
    assert las._reason_change_from_reply(query) == expected


@pytest.mark.parametrize("query,expected", [
    ("reason is bank work", "Bank work"),
    ("the new reason is dentist appointment", "Dentist appointment"),
    ("reason: bank work", "Bank work"),
    ("change the reason to family function", "Family function"),
])
def test_the_plainer_phrasings_still_work(query, expected):
    assert las._reason_change_from_reply(query) == expected


@pytest.mark.parametrize("query", [
    "yes", "no", "submit it", "apply sick leave for tomorrow",
    "what is my leave balance",
])
def test_an_unrelated_reply_is_not_read_as_a_reason_change(query):
    assert las._reason_change_from_reply(query) is None
