"""Regression tests for services/hr_graph.py's
_looks_like_regularisation_entry_content — the check that decides whether a
message mid-regularisation-flow is supplying entry content (a date, a time
range, or punch-tied reason wording) rather than switching topic.

Confirmed live: a regularisation entry's own reason text, e.g. "17 sep,
reason miss punch", contains the bare word "punch" with no other qualifier.
fast_classify's last-resort, single-word attendance fallback
(_ATTENDANCE_CHECK_ACTION_PATTERNS) matches that confidently as 'attendance'
— indistinguishable in shape from a genuine attendance question — and the
mid-flow regularisation guard in classify_node (hr_graph.py) used to let
that steal the turn away from regularisation_node, silently dropping the
entry. This check is deliberately independent of which intent fast_classify
guesses: it looks at the message's CONTENT, so it catches the false
positive regardless of whether the weak signal happens to land on
'attendance', 'punch_details', or anything else.
"""
import services.date_resolver as date_resolver
from services.hr_graph import (
    _looks_like_regularisation_entry_content,
    _looks_like_genuine_query,
)


def _content(query: str) -> bool:
    return _looks_like_regularisation_entry_content(
        query, date_resolver.mentions_date_expression(query)
    )


class TestRegularisationEntryContentDetection:

    def test_date_plus_bare_punch_reason_is_entry_content(self):
        assert _content("17 sep, reason miss punch") is True

    def test_date_plus_new_entry_after_prior_entry_is_complete(self):
        # The exact followup shape that used to be misrouted once an
        # earlier entry was already complete/awaiting confirmation.
        assert _content("also 17 sep, reason miss punch") is True

    def test_bare_reason_wording_without_a_date_is_entry_content(self):
        assert _content("reason miss punch") is True
        assert _content("missed punch") is True
        assert _content("forgot to punch out") is True
        assert _content("no punch that day") is True

    def test_time_range_variants_are_entry_content(self):
        assert _content("from 9am to 6pm") is True
        assert _content("from time 9 to time 6") is True
        assert _content("9 to 6, traffic delay") is True
        assert _content("10 se 7 bje mujhe late hua tha") is True

    def test_unrelated_reason_word_alone_is_not_entry_content(self):
        # "reason" alone, with no date/time/punch anchor, is too generic to
        # assume it's regularisation content — e.g. "reason for salary
        # deduction" has nothing to do with a punch entry.
        assert _content("what's the reason for my salary deduction") is False

    def test_genuinely_unrelated_message_is_not_entry_content(self):
        assert _content("what is my leave balance") is False
        assert _content("cancel my leave") is False

    def test_correction_language_naming_an_entry_field_is_entry_content(self):
        # Confirmed live/reported: sent right after "Shall I go ahead and
        # submit this?" (entry already complete, awaiting confirmation),
        # this used to fall through to the LLM fallback and get
        # misclassified as 'attendance_explain' since the message reads
        # like an attendance question ("reason"/"late") with no date, time
        # range, or punch-anchored reason wording to anchor it as
        # regularisation content.
        assert _content("change reason I was late") is True
        assert _content("change the reason to traffic delay") is True
        assert _content("update the end time to 7pm") is True
        assert _content("please correct the start time") is True


class TestGenuineQuestionGuardStillApplies:
    """_looks_like_regularisation_entry_content is always combined with
    "not _looks_like_genuine_query" by the caller in hr_graph.py — a real
    question phrased with a recognizable leading marker must still win,
    even if it happens to also contain regularisation-shaped content."""

    def test_leading_question_marker_is_recognized_as_genuine(self):
        assert _looks_like_genuine_query("why is my punch missing on the 17th") is True
        assert _looks_like_genuine_query("what is my leave balance for 17 sep") is True

    def test_bare_followup_has_no_leading_marker(self):
        assert _looks_like_genuine_query("17 sep, reason miss punch") is False
