from __future__ import annotations


def _create_chat(client, agent_id: str, node_id: str, ref: str) -> str:
    r = client.post(
        "/api/chats",
        json={
            "agent_id": agent_id,
            "node_id": node_id,
            "source": "web",
            "external_chat_ref": ref,
        },
    )
    assert r.status_code == 200, r.text
    return r.json()["chat"]["chat_id"]


def test_e2e_routes_send_to_noda1_via_infer(sofiia_client, sofiia_module, monkeypatch):
    calls = []

    def _router_url(node_id: str) -> str:
        return {
            "NODA1": "http://fake-noda1-router:9102",
            "NODA2": "http://fake-noda2-router:9102",
        }.get(str(node_id).upper(), "")

    async def _fake_infer(base_url, agent_id, text, **kwargs):
        calls.append(
            {
                "base_url": base_url,
                "agent_id": agent_id,
                "text": text,
                "metadata": kwargs.get("metadata") or {},
            }
        )
        return {
            "response": "fake-noda1-response",
            "backend": "fake-router",
            "model": "fake-model",
            "node_id": "NODA1",
            "message_id": "m-noda1",
        }

    monkeypatch.setattr(sofiia_module, "get_router_url", _router_url)
    monkeypatch.setattr(sofiia_module, "infer", _fake_infer)

    chat_id = _create_chat(sofiia_client, "monitor", "NODA1", "e2e-n1")
    r = sofiia_client.post(f"/api/chats/{chat_id}/send", json={"text": "ping NODA1"})
    assert r.status_code == 200, r.text
    body = r.json()

    assert len(calls) == 1
    assert calls[0]["base_url"] == "http://fake-noda1-router:9102"
    assert calls[0]["metadata"].get("chat_id") == chat_id
    assert calls[0]["metadata"].get("node_id") == "NODA1"
    assert body["node_id"] == "NODA1"


def test_e2e_routes_send_to_noda2_via_infer(sofiia_client, sofiia_module, monkeypatch):
    calls = []

    def _router_url(node_id: str) -> str:
        return {
            "NODA1": "http://fake-noda1-router:9102",
            "NODA2": "http://fake-noda2-router:9102",
        }.get(str(node_id).upper(), "")

    async def _fake_infer(base_url, agent_id, text, **kwargs):
        calls.append(
            {
                "base_url": base_url,
                "agent_id": agent_id,
                "text": text,
                "metadata": kwargs.get("metadata") or {},
            }
        )
        return {
            "response": "fake-noda2-response",
            "backend": "fake-router",
            "model": "fake-model",
            "node_id": "NODA2",
            "message_id": "m-noda2",
        }

    monkeypatch.setattr(sofiia_module, "get_router_url", _router_url)
    monkeypatch.setattr(sofiia_module, "infer", _fake_infer)

    chat_id = _create_chat(sofiia_client, "sofiia", "NODA2", "e2e-n2")
    r = sofiia_client.post(f"/api/chats/{chat_id}/send", json={"text": "ping NODA2"})
    assert r.status_code == 200, r.text
    body = r.json()

    assert len(calls) == 1
    assert calls[0]["base_url"] == "http://fake-noda2-router:9102"
    assert calls[0]["metadata"].get("chat_id") == chat_id
    assert calls[0]["metadata"].get("node_id") == "NODA2"
    assert body["node_id"] == "NODA2"


def test_e2e_noda1_route_is_not_hardcoded_to_noda2(sofiia_client, sofiia_module, monkeypatch):
    def _router_url(node_id: str) -> str:
        return {
            "NODA1": "http://fake-noda1-router:9102",
            "NODA2": "http://fake-noda2-router:9102",
        }.get(str(node_id).upper(), "")

    async def _guard_infer(base_url, agent_id, text, **kwargs):
        assert base_url != "http://fake-noda2-router:9102"
        return {"response": "guard-ok", "backend": "fake-router", "model": "fake-model"}

    monkeypatch.setattr(sofiia_module, "get_router_url", _router_url)
    monkeypatch.setattr(sofiia_module, "infer", _guard_infer)

    chat_id = _create_chat(sofiia_client, "monitor", "NODA1", "e2e-guard")
    r = sofiia_client.post(f"/api/chats/{chat_id}/send", json={"text": "guard ping"})
    assert r.status_code == 200, r.text
    assert r.json()["node_id"] == "NODA1"

