"""QA sheet, 07-Oct: 41 general and HR queries run as an employee and a
manager through the real graph. These are the ones that went wrong."""
import pytest

import services.ai_service as ai
import services.classify_node as cn
import services.hr_graph as hg
import services.punch_details as pd


class TestAWritingRequestIsNotALeaveApproval:
    """A manager's "draft a polite decline to a vendor proposal" matched
    "decline" and was answered "Which request would you like me to approve?"."""

    @pytest.mark.parametrize("query", [
        "draft a polite decline to a vendor proposal", "write a rejection email to a candidate",
        "can you draft an approval note for the budget",
    ])
    def test_not_an_approval(self, query):
        assert cn._is_leave_approval_query(query.lower()) is False

    @pytest.mark.parametrize("query", ["approve LR-32", "reject ayush's leave request"])
    def test_real_approvals_still_are(self, query):
        assert cn._is_leave_approval_query(query.lower()) is True


class TestOwnPay:
    """"what is my salary this month" printed "A None typically earns None in
    None." - and underneath it the model had read a payslip figure out of the
    company documents and streamed it as the employee's own net pay."""

    @pytest.mark.parametrize("query", ["what is my salary this month", "show my payslip",
                                       "how much do I take home", "what is my CTC"])
    def test_own_pay(self, query):
        assert hg._asks_for_own_pay(query) is True

    @pytest.mark.parametrize("query", ["what does a data analyst earn in pune",
                                       "average salary of a software engineer",
                                       "salary structure policy", "when is salary credited"])
    def test_not_own_pay(self, query):
        assert hg._asks_for_own_pay(query) is False

    async def test_answered_without_the_model(self, monkeypatch):
        def _never(*a, **k):
            raise AssertionError("the model was called for an own-pay question")
        monkeypatch.setattr(hg, "_call_llm_stream", _never)
        events = []
        out = await hg.hr_response_node(
            {"query": "what is my salary this month", "intent": "salary", "context": [],
             "usage_events": []}, events.append)
        assert out["response_text"] == hg.OWN_PAY_MESSAGE
        assert [e["content"] for e in events if e["type"] == "chunk"] == [hg.OWN_PAY_MESSAGE]

    def test_the_template_never_prints_none(self):
        msg = ai._build_user_message("salary", {"answer": "Your net salary is 83,501.40"})
        assert "None" not in msg and "83,501" not in msg

    def test_a_full_market_answer_keeps_its_template(self):
        msg = ai._build_user_message("salary", {"role": "Data Analyst", "salary_range": "6-9 LPA",
                                                "location": "Pune", "insights": ""})
        assert msg.startswith("A Data Analyst typically earns 6-9 LPA in Pune.")


class TestAReferenceIsNotAName:
    """A manager's "LR-32" was looked up as an employee, matched nobody, and
    was handed to general chat."""

    @pytest.mark.parametrize("query", ["LR-32", "lr 32", "REG-149"])
    def test_not_a_name(self, query):
        assert cn._is_bare_person_fragment(query, False) is False


class TestSimpleDays:
    """The model read "yesterday" as today in one of many identical runs."""

    @pytest.mark.parametrize("query, expected", [
        ("show my punch details for yesterday", True), ("punch details today", True),
        ("day before yesterday punches", True), ("yesterday and today punches", False),
        ("show punches for 2 days before yesterday", False), ("punches since yesterday", False),
    ])
    def test_names_one_simple_day(self, query, expected):
        assert pd._names_one_simple_day(query) is expected
