"""Regression tests for services/hr_graph.py's mid-flow regularisation
guard — specifically _looks_like_other_hr_topic and its effect on
is_regularisation_shaped_reply / is_low_signal_pending_reply.

Two requirements this closes:

1. A message naming a CLEARLY DIFFERENT HR topic while a regularisation
   entry is pending — "attendance for 20 aug" — must route to that OTHER
   intent (attendance, punch_details, leave_balance, ...), not get stuck
   answering "which date(s)" or re-showing the regularisation card.
   Confirmed live/reported: "attendance for 20 aug" has a bare date
   (has_date_expression=True), which alone used to satisfy
   _looks_like_regularisation_entry_content and kept it stuck in the
   regularisation flow even though fast_classify had already confidently
   identified it as 'attendance'.

2. A message that's genuinely supplying the regularisation REASON — "reason
   miss punch", "forgot to punch out", "missed punch" — must keep being
   recognized as regularisation content and NOT get diverted to another
   intent, even if it happens to contain a word also present in the
   other-topic list (reason wording always wins — see
   _looks_like_other_hr_topic's own docstring).
"""
import services.date_resolver as date_resolver
from services.nlp_service import fast_classify, _looks_like_genuine_query
from services.hr_graph import (
    _looks_like_regularisation_entry_content,
    _looks_like_other_hr_topic,
)


def _simulate(query: str, has_free_text_pending: bool):
    """Reproduces classify_node's own is_confident_interruption /
    is_low_signal_pending_reply computation for the mid-flow regularisation
    block, without needing a full graph invocation."""
    has_date_expression = date_resolver.mentions_date_expression(query)
    interrupt_intent, _, interrupt_confidence = fast_classify(query)
    other_topic = _looks_like_other_hr_topic(query)
    genuine = _looks_like_genuine_query(query)
    content = _looks_like_regularisation_entry_content(query, has_date_expression)

    reg_shaped = (not genuine) and (not other_topic) and content
    low_signal = (
        has_free_text_pending and not genuine and not other_topic
    ) or reg_shaped
    confident_interruption = bool(
        interrupt_intent
        and interrupt_intent != "regularisation"
        and interrupt_confidence >= 0.5
        and not low_signal
    )
    return confident_interruption, reg_shaped


class TestOtherTopicQueriesInterruptRegardlessOfPendingState:

    _QUERIES = [
        "attendance for 20 aug",
        "my attendance for 20 aug",
        "what is my attendance for 20 aug",
        "check in time for 20 aug",
        "leave balance",
        "check my leave status",
        "punch details for 20 aug",
        "timesheet for last week",
    ]

    def test_interrupts_while_an_entry_is_still_incomplete(self):
        for query in self._QUERIES:
            interrupted, _ = _simulate(query, has_free_text_pending=True)
            assert interrupted, f"{query!r} should have interrupted (pending=True)"

    def test_interrupts_once_every_entry_is_already_complete(self):
        for query in self._QUERIES:
            interrupted, _ = _simulate(query, has_free_text_pending=False)
            assert interrupted, f"{query!r} should have interrupted (pending=False)"


class TestReasonWordingStillStaysInRegularisation:

    _QUERIES = [
        "17 sep, reason miss punch",
        "also 17 sep, reason miss punch",
        "reason miss punch",
        "forgot to punch out",
        "missed punch",
        "10am to 7pm",
    ]

    def test_never_treated_as_an_interruption(self):
        for query in self._QUERIES:
            interrupted, _ = _simulate(query, has_free_text_pending=True)
            assert not interrupted, f"{query!r} should NOT have interrupted (pending=True)"
            interrupted, _ = _simulate(query, has_free_text_pending=False)
            assert not interrupted, f"{query!r} should NOT have interrupted (pending=False)"

    def test_reason_wording_wins_even_when_it_mentions_an_other_topic_word(self):
        # "attendance" appears in the list _looks_like_other_hr_topic
        # checks, but "missed punch" is unmistakably supplying a REASON, so
        # the reason-wording check must win over the other-topic exclusion.
        query = "missed punch, attendance system was down"
        assert _looks_like_other_hr_topic(query) is False


class TestBareDateContinuationIsUnaffected:
    """A bare date with nothing else ("20 aug", "17 sep") is the normal way
    to correct/select the entry's own date — must still be recognized as
    regularisation content, not swept up by the other-topic exclusion
    (neither query contains any other-topic word at all)."""

    def test_bare_date_is_still_regularisation_shaped(self):
        for query in ["20 aug", "17 sep"]:
            interrupted, reg_shaped = _simulate(query, has_free_text_pending=True)
            assert not interrupted
            assert reg_shaped
