"""A question about WHEN leave may be taken belongs to the policy documents.

Reported live: "can I take a leave on wednesday" stopped reaching the policy
docs. The classifier flapped between `policy` and `leave_eligibility`, and on
the eligibility side nothing rescued it -- a day name was not recognised as a
condition, so the eligibility node ran, found no leave type, and answered with
the menu of leave types. An answer to a question nobody asked.

The fix is deliberately in TWO places, because one of them is a language model
and cannot be relied on to land the same way twice:

  * the taxonomy says a WHEN-condition is `policy`, which is what makes the
    classifier steady;
  * _eligibility_question_is_really_a_rule treats a day, weekend or holiday as
    a condition, so even when the classifier says leave_eligibility the router
    still sends it to policy_rag_node.

That second half is the one that matters. These tests assert the ROUTE, not
the label, so they keep holding whichever way the model goes.
"""
import pytest

import services.hr_graph as hg

WHEN_QUESTIONS = [
    "can I take a leave on wednesday",
    "can i take a leave on wednesday?",
    "can I take leave on wednesday",
    "am I allowed to take a leave on wednesday",
    "can I take leave on a holiday",
    "are we allowed leave on weekends",
    "can i take a leave on friday",
    "is leave allowed on public holidays",
]

# Still the eligibility node's: these ask about the caller's own standing,
# which erp-api answers from their record and no document can.
PERSONAL_QUESTIONS = [
    "am I eligible for casual leave",
    "do I qualify for earned leave",
    "can I take leave if I am still on probation",
    "am I eligible for sick leave",
]


@pytest.mark.parametrize("query", WHEN_QUESTIONS)
@pytest.mark.parametrize("intent", ["leave_eligibility", "policy"])
def test_a_when_question_reaches_the_documents_either_way(query, intent):
    """The classifier is allowed to call this either thing; the route must be
    the same. This is what stops a model flip from losing the answer."""
    assert hg._route_after_classify({"query": query, "intent": intent}) == "policy_rag_node", (
        f"{query!r} labelled {intent!r} did not reach the policy documents")


@pytest.mark.parametrize("query", PERSONAL_QUESTIONS)
def test_a_question_about_the_person_stays_with_the_eligibility_node(query):
    assert hg._route_after_classify(
        {"query": query, "intent": "leave_eligibility"}) == "leave_eligibility_node"


@pytest.mark.parametrize("query", WHEN_QUESTIONS)
def test_the_day_is_read_as_a_condition(query):
    assert hg._eligibility_question_is_really_a_rule(query)


@pytest.mark.parametrize("query", [
    "apply leave on wednesday", "apply sick leave for tomorrow",
    "i want to apply for leave on wednesday",
])
def test_an_actual_application_is_untouched(query):
    """_eligibility_question_is_really_a_rule only ever runs on a question the
    classifier already called leave_eligibility, but assert the routing anyway:
    an application must reach the apply flow, not the documents."""
    assert hg._route_after_classify(
        {"query": query, "intent": "apply_leave"}) == "leave_action_node"
