"""QA LF-01 / LF-04 (26 Oct): summaries of a long chat.

LF-01  "Summarize what we discussed about yoga" (LT-05) and "Summary de do"
       (LT-06) covered only the last one or two turns. The classifier called
       them non_hr, and the general lane sends the model the last 6 messages;
       "Can you summarize everything we discussed?" was called
       conversation_meta, reached hr_response_node with the whole chat, and
       was complete. On top of that main.py loads 20 messages (10 exchanges),
       and the QA chats ran 13-15 turns before the summary.
LF-04  The model's own correction reached the user: "Shatavari (अश्वगंधा?
       actually शतावरी)".
"""
import pytest

import services.hr_graph as hr_graph
from services.ai_service import GENERAL_CHAT_SYSTEM_PROMPT, GENERAL_CHAT_WEB_SEARCH_PROMPT, HR_SYSTEM_PROMPT
from services.nlp_service import is_chat_summary_request, mentions_summary

from tests.test_bare_name_continuation import _mocks, _state, FakeWriter

SUMMARY_REQUESTS = [
    "Summarize what we discussed about yoga", "Summary de do",
    "Can you summarize everything we discussed?", "Summarize our conversation",
    "Summarize the Python topics we discussed", "Summarize what we discussed",
    "recap please", "sum it up", "what have we talked about so far?",
    "give me a summary of this chat", "summary", "summarize", "can you recap what we covered",
]

NOT_CHAT_SUMMARIES = [
    "show my attendance summary", "summary of my leaves", "summarize the leave policy",
    "team absent summary", "give me a summary of the sick leave policy",
    "what is the summary of the leave rules", "monthly attendance summary for my team",
]


@pytest.mark.parametrize("query", SUMMARY_REQUESTS)
def test_chat_summaries_are_recognised(query):
    assert is_chat_summary_request(query)


@pytest.mark.parametrize("query", NOT_CHAT_SUMMARIES)
def test_hr_summaries_are_not(query):
    assert not is_chat_summary_request(query)


@pytest.mark.asyncio
@pytest.mark.parametrize("query", ["Summarize what we discussed about yoga", "Summary de do"])
async def test_a_chat_summary_reaches_the_node_that_reads_the_whole_chat(query):
    with _mocks(detected="non_hr") as m:
        result = await hr_graph.classify_node(_state(query, last_intent="non_hr", is_manager=False),
                                              FakeWriter())

    assert result["intent"] == "conversation_meta"
    assert not m["classify"].called
    assert hr_graph._route_after_classify(result) == "hr_response_node"


def _chat(turns):
    ctx = []
    for i in range(turns):
        ctx += [{"role": "user", "content": f"question {i}"}, {"role": "assistant", "content": f"answer {i}"}]
    return ctx


def test_the_general_lane_keeps_its_short_window_for_ordinary_questions():
    out = hr_graph._general_chat_context({"query": "can beginners do it?", "context": _chat(10)})
    assert len(out) == hr_graph._GENERAL_CHAT_CONTEXT_MESSAGES


def test_the_general_lane_reads_the_whole_chat_for_a_summary():
    """"Summarize the trip plan" names no chat, so the classifier decides -
    and if it lands here it must still see every turn (LT-03)."""
    assert not is_chat_summary_request("Summarize the trip plan")
    assert mentions_summary("Summarize the trip plan")

    out = hr_graph._general_chat_context({"query": "Summarize the trip plan", "context": _chat(12)})

    assert len(out) == 24
    assert out[0]["content"] == "question 0"


@pytest.mark.parametrize("prompt", [HR_SYSTEM_PROMPT, GENERAL_CHAT_SYSTEM_PROMPT, GENERAL_CHAT_WEB_SEARCH_PROMPT])
def test_prompts_forbid_showing_self_corrections(prompt):
    assert "Never show a correction or doubt of your own" in prompt


def test_the_hr_prompt_asks_for_every_turn_of_the_topic():
    assert "cover EVERY turn in it" in HR_SYSTEM_PROMPT
    assert "include every turn on that topic, however far back" in HR_SYSTEM_PROMPT
