"""Team-attendance classification for absence/presence phrasing.

Reported case: a manager asking "Who was absent in my team on 20 August 2026?"
matched none of the _TEAM_ATTENDANCE_PATTERNS (all three require an "atten..."
token), so it skipped the deterministic fast-path entirely and the answer
depended on the main LLM classifier landing on HRMS_TEAM_ATTENDANCE_QUERY.

The negative cases matter as much as the positive ones: this route must not
swallow personal attendance questions, or team LEAVE questions, both of which
are owned by other intents with their own date handling.
"""

import pytest

from services.classify_node import _is_team_attendance_query
from services.hr_graph import _resolve_team_attendance_date


@pytest.mark.parametrize("query", [
    # The reported case, and the phrasings around it.
    "who was absent in my team on 20 august 2026?",
    "who was absent in my team today",
    "who is absent in my team",
    "who was present in my team on 20 august",
    "absentees in my team yesterday",
    "which team members were absent on 20 august 2026",
    "who didn't come in today in my team",
    "show me the absence in my team last monday",
    # Pre-existing phrasings must keep working.
    "my team attendance",
    "team attendance on 20 august 2026",
    "attendance for my team",
])
def test_team_level_attendance_questions_route_deterministically(query):
    assert _is_team_attendance_query(query) is True


@pytest.mark.parametrize("query,why", [
    ("was i absent on monday", "personal attendance, not the team's"),
    ("am i absent today", "personal attendance, not the team's"),
    ("who was on leave in my team", "owned by HRMS_TEAM_LEAVE_QUERY"),
    ("my team leave requests", "owned by HRMS_TEAM_LEAVE_QUERY"),
    ("my team shift roster", "owned by shift_query"),
    ("my team salary details", "not an attendance question"),
    ("regularisation requests for my team", "owned by manager_regularisation_view"),
    ("i need to give a presentation to my team", "'presentation' is not 'present'"),
    ("what is my attendance", "personal attendance, no team reference"),
])
def test_other_questions_are_left_to_their_own_intents(query, why):
    assert _is_team_attendance_query(query) is False, why


@pytest.mark.parametrize("query", [
    # Reported case: "ask for team attendance details/summary" was answered
    # with the Apply-Regularization card. These all put "attendance" BEFORE
    # the team reference, which the original third pattern could not read
    # because it required the two to be adjacent.
    "attendance details of my team",
    "show me attendance details of my team",
    "give me the attendance summary for my team",
    "attendance report for my team",
    "attendance summary across my team",
    "attendance of my team",
    "show attendance summary of team",
    "attendance for the team",
])
def test_attendance_before_the_team_reference_still_routes_to_team_attendance(query):
    assert _is_team_attendance_query(query) is True


@pytest.mark.parametrize("query", [
    "attendance details of my team",
    "give me the attendance summary for my team",
    "attendance report for my team",
])
def test_fast_classify_agrees_it_is_a_team_question(query):
    """fast_classify drives the mid-flow interruption and fallback paths, so a
    disagreement with classify_node is not cosmetic — it decides which node
    answers. This wording used to score as PERSONAL `attendance`."""
    from services.nlp_service import fast_classify
    intent, is_hr, _confidence = fast_classify(query)
    assert intent == "HRMS_TEAM_ATTENDANCE_QUERY", query
    assert is_hr is True


@pytest.mark.parametrize("query", [
    "what is my attendance",
    "my attendance details",
    "attendance details",
])
def test_personal_attendance_is_not_captured_by_the_team_route(query):
    from services.nlp_service import fast_classify
    assert _is_team_attendance_query(query) is False
    assert fast_classify(query)[0] == "attendance"


@pytest.mark.parametrize("query", [
    "what is the attendance policy for my team",
    "attendance rules for my team",
])
def test_policy_questions_are_not_answered_with_a_live_summary(query):
    """Widening the pattern to allow filler words would otherwise swallow
    these: they mention attendance and the team, but want the policy text."""
    assert _is_team_attendance_query(query) is False


def test_an_explicit_past_date_is_not_silently_swapped_for_today():
    """The other half of the reported bug: a named date must survive to the
    erp-api call, or the card ends up headed "Today" for a manager who asked
    about three weeks ago."""
    from datetime import date
    today = date(2026, 9, 16)

    for query in (
        "who was absent in my team on 20 august 2026?",
        "who was absent in my team on 20 aug 2026",
        "who was absent in my team on 20/08/2026",
    ):
        assert _resolve_team_attendance_date(query, today) == "2026-08-20", query


def test_a_query_with_no_date_still_defaults_to_today():
    """Today is for a query with NO date reference — the default must survive
    the fix above."""
    from datetime import date
    today = date(2026, 9, 16)
    assert _resolve_team_attendance_date("who was absent in my team", today) is None


# Reported live: "show my team members attendence" answered "I'm unable to
# reach the HR system right now". The misspelling failed every team-attendance
# pattern, so the roster hook claimed it off "my team members" and the query
# ran the team MEMBERS node instead. Both classifiers must agree, since
# main.py seeds the graph from fast_classify.
@pytest.mark.parametrize("query", [
    "show my team members attendence",
    "show my team members attendance",
    "show my team attendence",
    "team member attendance",
    "my team members' attendance today",
])
def test_team_members_attendance_is_attendance_not_the_roster(query):
    from services.classify_node import _is_team_members_query
    from services.nlp_service import fast_classify

    assert _is_team_attendance_query(query) is True
    assert _is_team_members_query(query) is False
    assert fast_classify(query)[0] == "HRMS_TEAM_ATTENDANCE_QUERY"


@pytest.mark.parametrize("query", ["show my team members", "show my team", "who is on my team"])
def test_plain_roster_questions_still_reach_the_roster(query):
    from services.classify_node import _is_team_members_query

    assert _is_team_attendance_query(query) is False
    assert _is_team_members_query(query) is True
