"""Writing an email is a general-chat task.

Live: "Write a welcome email" was classified `onboarding` and answered as an
HR topic; "Write a leave application email to my manager" opened the apply-
leave flow. With general chat on, any request to write an email goes to the
general lane. With it off, nothing changes.
"""
import pytest

import services.classify_node as classify_node


@pytest.fixture
def llm_says_onboarding(monkeypatch):
    """What the live classifier answered for "Write a welcome email"."""
    async def _no_shift(q):
        return None, 0.0, None

    async def _classify(prompt, labels, fallback_label="unknown"):
        return "onboarding", None, False

    monkeypatch.setattr(classify_node.ai_service, "classify_shift_intent", _no_shift)
    monkeypatch.setattr(classify_node, "classify_intent_structured", _classify)


async def _route(query, general_chat_enabled=True, is_manager=False):
    out = await classify_node.classify_node(
        {"query": query, "company_id": 1, "conversation_id": f"e-{abs(hash(query))}",
         "bearer_token": "t", "usage_events": [], "is_manager": is_manager,
         "last_intent": None, "general_chat_enabled": general_chat_enabled},
        lambda e: None)
    return out["intent"], out["is_hr"]


EMAILS = [
    "Write a welcome email", "Write a professional follow-up email to a customer.",
    "follow-up email for the client", "draft a reply mail to my vendor",
    "help me compose an email to the team", "resignation email",
    "Write a leave application email to my manager for tomorrow",
]


@pytest.mark.parametrize("query", EMAILS)
@pytest.mark.parametrize("is_manager", [False, True])
async def test_an_email_request_goes_to_general_chat(llm_says_onboarding, query, is_manager):
    assert await _route(query, is_manager=is_manager) == ("non_hr", False)


@pytest.mark.parametrize("query", EMAILS[:2])
async def test_with_general_chat_off_nothing_changes(llm_says_onboarding, query):
    intent, is_hr = await _route(query, general_chat_enabled=False)
    assert (intent, is_hr) == ("onboarding", True)


@pytest.mark.parametrize("query", [
    "what is the HR email id", "update my email address", "check my mail",
    "write a leave policy", "apply leave for tomorrow", "show my payslip email",
])
def test_not_an_email_writing_request(query):
    assert classify_node._is_email_writing_request(query) is False
