"""Recognising a NAMED employee in a leave lookup — the deterministic half.

FR-07.3 routes "show <someone> pending leaves" to manager_leave_request_lookup.
Whether it gets there deterministically is decided by _score_individual_report_
query, and that in turn by _has_third_party_subject.

The live bug these lock down: "show geo3 pending leave" scored 0.9 and routed,
while "show geo3 pending leaveS" scored 0.0 and fell through to the LLM — the
pattern ended `leave\\b`, which cannot match the plural. With no deterministic
signal the label came from the LLM alone, so two near-identical messages
reached different nodes, and the run-to-run answer for the SAME message was
not stable either.

Landing on HRMS_TEAM_LEAVE_QUERY is worse than a mislabel: team_leave_query_
node sends no employee_name, so the named employee is silently dropped and the
manager gets the whole team's list instead — a plausible answer to a question
nobody asked.

The self/team cases here are the other half of the contract. This pattern must
never claim a question about the caller's own leave, nor one about the whole
team, so every widening is paired with the non-regressions it must not cause.
"""

import pytest

import services.classify_node as classify_node


IR_LEAVE = "manager_leave_request_lookup"


def score(query: str):
    return classify_node._score_individual_report_query(query.lower())


def label(query: str):
    return score(query)[0]


# ═════════════════════════════════════════════════════════════════════════════
# The plural — the regression that started this
# ═════════════════════════════════════════════════════════════════════════════

class TestPluralLeaves:

    @pytest.mark.parametrize("query", [
        "show geo3 pending leaves",
        "show geo3 pending leaves request",
        "check geo3 pending leaves",
        "get geo3 pending leaves",
        "display geo3 pending leaves",
    ])
    def test_plural_is_recognised(self, query):
        assert label(query) == IR_LEAVE

    @pytest.mark.parametrize("query", [
        "show geo3 pending leave",
        "show geo3 leave request",
    ])
    def test_singular_still_works(self, query):
        assert label(query) == IR_LEAVE

    def test_singular_and_plural_agree(self):
        """The whole point: one trailing "s" must not change the route."""
        assert label("show geo3 pending leave") == label("show geo3 pending leaves")

    def test_routes_deterministically_not_by_llm(self):
        """Above _IR_ROUTE_CONFIDENCE means classify_node returns on the
        pattern alone — no adjudication call, and no run-to-run variation."""
        _, confidence = score("show geo3 pending leaves")
        assert confidence >= classify_node._IR_ROUTE_CONFIDENCE


# ═════════════════════════════════════════════════════════════════════════════
# Names that cost more than one word
# ═════════════════════════════════════════════════════════════════════════════

class TestNameAndStatusBothFit:

    @pytest.mark.parametrize("query", [
        "show B Geo pending leaves",
        "show Priya Nair pending leaves",
        "show B Geo pending leave",
    ])
    def test_two_word_name_plus_status(self, query):
        """The name budget is two words; a status word used to consume one of
        them, so a real first+last name plus "pending" overflowed it."""
        assert label(query) == IR_LEAVE

    @pytest.mark.parametrize("status", ["pending", "cancelled", "canceled"])
    def test_each_status_adjective_is_skipped(self, status):
        assert label(f"show geo3 {status} leaves") == IR_LEAVE

    def test_bare_employee_code_is_recognised_as_a_third_party(self):
        """geo3 is the emp_no shape this deployment uses. It carries no
        possessive and no "employee" keyword, so this pattern is the only one
        that can see it.

        Recognising the person is necessary but not sufficient: scoring also
        needs a leave MARKER, and a bare "leaves" is deliberately not one —
        the conjunction is what keeps the pattern from claiming every message
        containing a name and the word leave."""
        assert classify_node._has_third_party_subject("show geo3 leaves") is True
        assert score("show geo3 leaves") == (None, 0.0)
        assert label("show geo3 pending leaves") == IR_LEAVE


# ═════════════════════════════════════════════════════════════════════════════
# Non-regressions: the caller's own leave
# ═════════════════════════════════════════════════════════════════════════════

class TestOwnLeaveIsNeverClaimed:

    @pytest.mark.parametrize("query", [
        "show my pending leaves",
        "show me my leaves",
        "show my leaves",
        "show my approved leaves",
        "show me my pending leave requests",
    ])
    def test_first_person_is_vetoed(self, query):
        assert score(query) == (None, 0.0)

    @pytest.mark.parametrize("query", [
        "show pending leaves",
        "show all leaves",
        "show any leaves",
    ])
    def test_unnamed_lookup_is_not_a_named_lookup(self, query):
        """No person named — the negative lookahead blocks pending/all/any in
        the name position, which is what keeps these out."""
        assert score(query) == (None, 0.0)


# ═════════════════════════════════════════════════════════════════════════════
# Non-regressions: the whole team
# ═════════════════════════════════════════════════════════════════════════════

class TestTeamQueriesAreNeverClaimed:

    @pytest.mark.parametrize("query", [
        "show pending leave requests for my team",
        "show approved leaves for my team",
        "show cancelled leaves for my team",
        "show all leave requests for my team",
        "show team leaves",
        "my team's leave requests",
    ])
    def test_team_route_is_untouched(self, query):
        assert score(query) == (None, 0.0)


# ═════════════════════════════════════════════════════════════════════════════
# The leave-status vocabulary
# ═════════════════════════════════════════════════════════════════════════════

class TestLeaveStatusMarkers:

    @pytest.mark.parametrize("query", [
        "show geo3 cancelled leaves",
        "show geo3 canceled leaves",
        "show geo3 leave history",
    ])
    def test_non_pending_statuses_score(self, query):
        assert label(query) == IR_LEAVE

    def test_approved_status_is_still_pre_empted_by_the_approval_veto(self):
        """Documents a KNOWN GAP rather than asserting desired behaviour.

        _IR_SIBLING_ROUTE_MARKERS vetoes on the bare substring "approve",
        which "approved leaves" contains, so "show geo3 approved leaves"
        scores 0.0 even though _is_leave_approval_query already said it is
        not an approval instruction. When that veto is narrowed to the verb
        forms, this test should flip to asserting IR_LEAVE.
        """
        assert classify_node._is_leave_approval_query("show geo3 approved leaves") is False
        assert classify_node._has_third_party_subject("show geo3 approved leaves") is True
        assert score("show geo3 approved leaves") == (None, 0.0)


# ═════════════════════════════════════════════════════════════════════════════
# Sibling routes the pattern must still stand down for
# ═════════════════════════════════════════════════════════════════════════════

class TestSiblingRoutesStillWin:

    @pytest.mark.parametrize("query", [
        "approve geo3 leave",
        "reject geo3 leave request",
    ])
    def test_approval_instructions_are_not_claimed(self, query):
        assert score(query) == (None, 0.0)

    def test_shift_questions_are_not_claimed(self):
        assert score("show geo3 shift") == (None, 0.0)
