""""all" answered to the "which employee?" card.

Reported: a mistyped name ("rudraveer111") brought up the roster card - "No
employee matched that name. Please select an employee from your team" - and
the manager replied "all". Back came a grey bubble with no intent and no
card:

    You have 2 pending leave request(s):
    1. Rudraveer (FNF-004), Casual Leave, 2026-09-21 ...
    2. Rudraveer (FNF-004), Sick Leave, 2026-09-23 ...

That sentence is nowhere in this codebase - the chat model wrote it, because
"all" matched no candidate and the turn fell through to general
classification. Note it also says "YOU have", about requests belonging to
Rudraveer: improvising from context gets whose-is-whose wrong, which is the
part that matters more than the missing card.

"all" is not a pick from the list, but it is a clear answer to it. It routes
to the TEAM view of whatever was being asked - the only place that answers
for more than one person - and clears the shortlist on the way, since the
turn is leaving the feature that armed it.
"""

import pytest

import services.hr_graph as hr_graph
import services.individual_report as individual_report
from services.individual_report import resolve_pending_selection_intent


CANDIDATES = [
    {"employee_id": 489, "employee_name": "ABC Testing", "employee_number": "test07"},
    {"employee_id": 765, "employee_name": "B Sharma", "employee_number": "bs1"},
    {"employee_id": 233, "employee_name": "Rudraveer", "employee_number": "FNF-004"},
]


@pytest.fixture
def report_store(monkeypatch):
    store = {}
    monkeypatch.setattr(individual_report.leave_slot_service, "get_report_state",
                        lambda c, k: store.get((str(c), k)))
    monkeypatch.setattr(individual_report.leave_slot_service, "set_report_state",
                        lambda c, k, v: bool(store.__setitem__((str(c), k), v)) or True)
    monkeypatch.setattr(individual_report.leave_slot_service, "clear_report_state",
                        lambda c, k: store.pop((str(c), k), None))
    return store


def arm(report_store, intent=individual_report.LEAVE_REQUEST_INTENT):
    report_store[("1", "conv-1")] = {
        "step": "awaiting_employee_selection",
        "intent": intent,
        "candidates": list(CANDIDATES),
    }
    return report_store


class TestAllGoesToTheTeamView:

    @pytest.mark.parametrize("reply", [
        "all", "All", "ALL", "everyone", "everybody", "all of them",
        "show all", "list all", "all employees", "all team members",
        "show me all of them", "all.",
    ])
    def test_it_routes_to_the_team_leave_view(self, report_store, reply):
        arm(report_store)

        assert resolve_pending_selection_intent(1, "conv-1", reply) == \
            "HRMS_TEAM_LEAVE_QUERY"

    def test_it_reaches_the_node_that_can_answer_for_everyone(self, report_store):
        """The team node is the only one that answers for more than one
        person - which is exactly what "all" asked for."""
        arm(report_store)
        intent = resolve_pending_selection_intent(1, "conv-1", "all")

        assert hr_graph._route_after_classify({"intent": intent}) == "team_leave_query_node"

    def test_an_attendance_card_goes_to_team_attendance(self, report_store):
        """"all" means the team view of whatever was being asked, not always
        leave."""
        arm(report_store, individual_report.ATTENDANCE_EXPLANATION_INTENT)

        intent = resolve_pending_selection_intent(1, "conv-1", "all")
        assert intent == "HRMS_TEAM_ATTENDANCE_QUERY"
        assert hr_graph._route_after_classify({"intent": intent}) == \
            "team_attendance_summary_node"

    def test_the_shortlist_is_cleared(self, report_store):
        """The turn is leaving this feature, so its card must not capture the
        next message."""
        arm(report_store)
        resolve_pending_selection_intent(1, "conv-1", "all")

        assert report_store.get(("1", "conv-1")) is None

    def test_it_never_lands_in_general_chat(self, report_store):
        arm(report_store)
        intent = resolve_pending_selection_intent(1, "conv-1", "all")

        assert intent is not None
        assert hr_graph._route_after_classify({"intent": intent}) != "hr_response_node"


class TestPickingOneIsUnchanged:

    @pytest.mark.parametrize("reply", ["Rudraveer", "B Sharma", "ABC Testing",
                                       "2", "the second one", "test07"])
    def test_a_named_pick_still_reaches_the_individual_lookup(self, report_store, reply):
        arm(report_store)

        assert resolve_pending_selection_intent(1, "conv-1", reply) == \
            individual_report.LEAVE_REQUEST_INTENT

    def test_a_named_pick_leaves_the_shortlist_armed(self, report_store):
        """Only "all" clears it - a pick is answered inside this feature,
        which still needs the state."""
        arm(report_store)
        resolve_pending_selection_intent(1, "conv-1", "Rudraveer")

        assert report_store[("1", "conv-1")]["step"] == "awaiting_employee_selection"


class TestWhatMustNotBeHijacked:

    @pytest.mark.parametrize("reply", [
        "all my leaves",
        "what is my leave balance",
        "show all my pending leaves",
        "how do I apply for leave",
        "cancel",
    ])
    def test_these_fall_through_to_normal_classification(self, report_store, reply):
        """"all MY leaves" is about the caller, not the team - anchoring the
        pattern is what keeps a stray "all" from taking the turn."""
        arm(report_store)

        assert resolve_pending_selection_intent(1, "conv-1", reply) is None

    def test_nothing_pending_means_nothing_to_answer(self, report_store):
        assert resolve_pending_selection_intent(1, "conv-1", "all") is None

    def test_a_fall_through_leaves_the_shortlist_intact(self, report_store):
        """A manager who changes the subject should still be able to come
        back and pick."""
        arm(report_store)
        resolve_pending_selection_intent(1, "conv-1", "what is my leave balance")

        assert report_store[("1", "conv-1")]["step"] == "awaiting_employee_selection"
