"""Two different actions in one message are asked about, not half-done
(negative run, 28 Oct 2026, #42).

"apply sick leave tomorrow and cancel my 2 nov leave" reached ONE flow and
the other half was dropped - the apply half on one run, the cancel half on
another, which also answered "couldn't find a leave request matching that"
about a 2 Nov leave that exists. compound_query.split() never splits an
action (each needs its own turn and confirmation), so the turn now asks
which to do first and offers each half as a reply.
"""
from unittest.mock import AsyncMock

import pytest
from langgraph.graph import END

import services.classify_node as classify_node
import services.hr_graph as hr_graph
from services.compound_query import two_actions

TWO = [
    ("apply sick leave tomorrow and cancel my 2 nov leave",
     ("apply sick leave tomorrow", "cancel my 2 nov leave")),
    ("cancel my 2 nov leave, then apply casual leave for 5 nov",
     ("cancel my 2 nov leave", "apply casual leave for 5 nov")),
    ("please apply leave for friday and also cancel my monday leave",
     ("please apply leave for friday", "cancel my monday leave")),
    ("approve dileep leave and reject priya leave", ("approve dileep leave", "reject priya leave")),
    ("regularize 20 oct and apply leave for 21 oct", ("regularize 20 oct", "apply leave for 21 oct")),
]
ONE = [
    "apply leave for 2 nov and 3 nov", "apply leave because I need to cancel my trip",
    "cancel my leave", "apply casual leave on 5 nov full day reason personal work",
    "approve dileep and priya leaves", "yes and cancel my other leave",
    "what is my balance and who invented radio", "apply leave and submit it",
    "cancel my 2 nov leave and 3 nov leave", "apply leave for tomorrow, reason: cancel plans",
]


@pytest.mark.parametrize("query,parts", TWO)
def test_two_actions_are_found_with_each_half_as_typed(query, parts):
    assert two_actions(query) == parts


@pytest.mark.parametrize("query", ONE)
def test_one_action_or_a_reason_is_not_two(query):
    assert two_actions(query) is None


class _Writer(list):
    def __call__(self, e):
        self.append(e)


async def test_the_turn_asks_which_first_without_the_classifier(monkeypatch):
    classify = AsyncMock(side_effect=AssertionError("classified a message it should have asked about"))
    monkeypatch.setattr(classify_node, "classify_intent_structured", classify)
    writer = _Writer()
    out = await classify_node.classify_node(
        {"query": TWO[0][0], "company_id": 1, "conversation_id": "two", "bearer_token": "t",
         "usage_events": [], "is_manager": False, "context": [], "date_typo_checked": True,
         "safety_checked": True}, writer)
    assert out["intent"] == classify_node.TWO_ACTIONS_CLARIFY_INTENT
    done = [e for e in writer if e.get("type") == "done"][-1]
    assert done["intent"] == "clarify_needed"
    assert done["data"]["options"] == list(TWO[0][1])
    assert "one of those at a time" in done["data"]["answer"]


def test_the_graph_ends_the_turn_there():
    assert hr_graph._route_after_classify({"intent": "two_actions_clarify", "query": ""}) == END
