"""QA: "show my tram attendacne for 22nsep2026" was the caller's OWN
attendance on some runs and the team's on others.

"tram" and "attendacne" matched no deterministic rule, so the LLM alone
decided. And "22nsep2026" matched no date pattern, so even the team route
would have answered for today. Both are folded before matching now.
"""

from datetime import date

import pytest

import services.classify_node as classify_node
import services.date_resolver as date_resolver
from services.hr_graph import _resolve_team_attendance_date
from services.nlp_service import fast_classify, fold_attendance_typos

TODAY = date(2026, 9, 24)


@pytest.mark.parametrize("query,folded", [
    ("show my tram attendacne for 22nsep2026", "show my team attendance for 22nsep2026"),
    ("show my teem attendance today", "show my team attendance today"),
    ("my atendance yesterday", "my attendance yesterday"),
    ("tram leave requests", "team leave requests"),
])
def test_team_and_attendance_typos_fold(query, folded):
    assert fold_attendance_typos(query) == folded


@pytest.mark.parametrize("word", ["tram", "teem", "taem", "tem", "tean", "temm", "teamm", "tema",
                                  "teaam", "yeam", "tesm", "twam", "taam", "teqm", "eam", "tram's"])
def test_any_one_slip_from_team_is_team(word):
    assert fast_classify(f"show my {word} attendance for 22 sep")[0] == "HRMS_TEAM_ATTENDANCE_QUERY"


@pytest.mark.parametrize("query", ["take the tram to office", "who is the attendant",
                                   "i attended the meeting", "the tram is late",
                                   "long term leave policy", "apply long term leave",
                                   "the tea leaves", "show your attendance", "total attendance today"])
def test_real_words_are_left_alone(query):
    assert fold_attendance_typos(query) == query


@pytest.mark.parametrize("query,intent", [
    ("show my tram attendacne for 22nsep2026", "HRMS_TEAM_ATTENDANCE_QUERY"),
    ("show my teem attendance today", "HRMS_TEAM_ATTENDANCE_QUERY"),
    ("tram attendance 22 sep", "HRMS_TEAM_ATTENDANCE_QUERY"),
    ("show my attendacne for 22nsep2026", "attendance"),     # no "team" - their own
    ("my atendance yesterday", "attendance"),
])
def test_routing(query, intent):
    assert fast_classify(query)[0] == intent


@pytest.mark.parametrize("query", ["show my tram attendacne for 22nsep2026", "show my teem attendance today"])
def test_classify_node_team_check_reads_the_folded_text(query):
    assert classify_node._is_team_attendance_query(fold_attendance_typos(query.lower()))


@pytest.mark.parametrize("text,iso", [
    ("22nsep2026", "2026-09-22"), ("22sep2026", "2026-09-22"), ("22sep 2026", "2026-09-22"),
    ("sep22 2026", "2026-09-22"), ("22-sep-2026", "2026-09-22"), ("21stmay", "2026-05-21"),
])
def test_glued_dates_resolve(text, iso):
    assert date_resolver.resolve_explicit_calendar_date(text, TODAY) == iso
    assert date_resolver.mentions_date_expression(text)


@pytest.mark.parametrize("text", ["3 days", "i have 2 mangoes", "covid19 policy", "top 10 employees",
                                  "5 decks"])
def test_numbers_that_are_not_dates_stay_that_way(text):
    assert date_resolver.unglue_dates(text) == text
    assert date_resolver.resolve_explicit_calendar_date(text, TODAY) is None


def test_the_team_node_answers_the_named_day_not_today():
    assert _resolve_team_attendance_date("show my tram attendacne for 22nsep2026", TODAY) == "2026-09-22"
