from contextlib import asynccontextmanager
from dataclasses import replace

import httpx
import pytest
from conftest import ScriptedModel
from langchain_core.messages import AIMessage

from dataagent.config.settings import A2AAgentSettings, A2ASettings
from dataagent.extensions.a2a import A2ACaller, A2ACallError, load_a2a_tools, remote_agent_tool
from dataagent.server.a2a import create_a2a_server


@asynccontextmanager
async def serving(runtime, model, **kwargs):
    app = create_a2a_server(runtime, host="127.0.0.1", port=9999, model=model, **kwargs)
    transport = httpx.ASGITransport(app=app)
    async with app.router.lifespan_context(app):
        async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:9999") as http:
            yield http


async def test_caller_round_trip_keeps_context(runtime):
    model = ScriptedModel(
        responses=[
            AIMessage(content="first"),
            AIMessage(content="second"),
        ]
    )
    async with serving(runtime, model) as http:
        caller = A2ACaller("http://127.0.0.1:9999", http_client=http)
        first = await caller.send("one")
        second = await caller.send("two")
    assert first.text == "first"
    assert first.state == "TASK_STATE_COMPLETED"
    assert second.text == "second"
    assert second.context_id == first.context_id == caller.context_id
    humans = [item.content for item in model.requests[1] if getattr(item, "type", None) == "human"]
    assert humans == ["one", "two"]


async def test_failed_task_raises(runtime):
    async with serving(runtime, ScriptedModel(responses=[])) as http:
        caller = A2ACaller("http://127.0.0.1:9999", http_client=http)
        with pytest.raises(A2ACallError, match="ran out of responses"):
            await caller.send("hello")


async def test_remote_diagnostics_and_injected_client_auth_are_preserved(runtime):
    async with serving(runtime, ScriptedModel(responses=[]), auth_token="peer-token") as http:
        caller = A2ACaller("http://127.0.0.1:9999", auth_token="peer-token", http_client=http)
        with pytest.raises(A2ACallError) as caught:
            await caller.send("hello")
    error = caught.value
    assert error.error_code == "TOOL_ERROR"  # Local tool classification remains compatible.
    assert error.remote_code == "INTERNAL_ERROR"
    assert error.request_id and error.task_id and error.context_id and error.root_run_id


async def test_injected_http_client_keeps_its_timeout_settings(runtime):
    timeouts = []

    async def observe(request):
        timeouts.append(request.extensions["timeout"])

    async with serving(runtime, ScriptedModel(responses=[AIMessage(content="ok")])) as http:
        http.timeout = httpx.Timeout(0.3, read=0.7)
        http.event_hooks["request"].append(observe)
        result = await A2ACaller("http://127.0.0.1:9999", timeout=180, http_client=http).send(
            "hello"
        )
    assert result.text == "ok"
    assert len(timeouts) >= 2  # Discovery and SendMessage both use the injected client.
    assert all(value["read"] == 0.7 and value["connect"] == 0.3 for value in timeouts)


@pytest.mark.parametrize(("configured_timeout", "expected"), [(None, 900), (17, 17)])
async def test_peer_http_timeout_is_independent_of_run_limit(
    runtime, monkeypatch, configured_timeout, expected
):
    app = create_a2a_server(
        runtime,
        host="127.0.0.1",
        port=9999,
        model=ScriptedModel(responses=[AIMessage(content="ok")]),
    )
    client_cls = httpx.AsyncClient
    read_timeouts = []

    async def observe(request):
        read_timeouts.append(request.extensions["timeout"]["read"])

    def client(**kwargs):
        return client_cls(
            **kwargs, transport=httpx.ASGITransport(app=app), event_hooks={"request": [observe]}
        )

    peer_settings = A2AAgentSettings(
        id="researcher", url="http://127.0.0.1:9999", timeout_seconds=configured_timeout
    )
    settings = runtime.settings.model_copy(
        update={
            "a2a": A2ASettings(remote_agents=(peer_settings,)),
            "server": runtime.settings.server.model_copy(update={"run_timeout_seconds": 60}),
        }
    )
    (tool,) = load_a2a_tools(replace(runtime, settings=settings))
    async with app.router.lifespan_context(app):
        monkeypatch.setattr(httpx, "AsyncClient", client)
        assert await tool.ainvoke({"message": "one"}) == "ok"

    assert read_timeouts
    assert set(read_timeouts) == {expected}


async def test_old_peer_without_diagnostic_metadata_still_works():
    from a2a.helpers import new_task
    from a2a.types.a2a_pb2 import SendMessageResponse, TaskState

    def respond(_message):
        return SendMessageResponse(
            task=new_task(
                "old-task",
                "old-context",
                TaskState.TASK_STATE_FAILED,
            )
        )

    async with peer(respond) as http:
        with pytest.raises(A2ACallError) as caught:
            await A2ACaller("http://127.0.0.1:9999", http_client=http).send("hello")
    assert caught.value.remote_code is None
    assert caught.value.request_id is None
    assert caught.value.task_id == "old-task"


async def test_blank_message_is_rejected_locally():
    caller = A2ACaller("http://127.0.0.1:9999")
    with pytest.raises(ValueError, match="must not be blank"):
        await caller.send("   ")


def test_agent_id_must_be_a_tool_name():
    with pytest.raises(ValueError, match="agent id"):
        remote_agent_tool("bad id", "http://127.0.0.1:9999")


async def test_remote_tool_keeps_one_context_per_local_thread(runtime):
    model = ScriptedModel(
        responses=[
            AIMessage(content="a1"),
            AIMessage(content="a2"),
            AIMessage(content="b1"),
        ]
    )
    async with serving(runtime, model) as http:
        tool = remote_agent_tool("researcher", "http://127.0.0.1:9999", http_client=http)
        assert "config" not in tool.args
        await tool.ainvoke(
            {"message": "one"},
            config={"configurable": {"thread_id": "local-a"}},
        )
        await tool.ainvoke(
            {"message": "two"},
            config={"configurable": {"thread_id": "local-a"}},
        )
        await tool.ainvoke(
            {"message": "three"},
            config={"configurable": {"thread_id": "local-b"}},
        )

    def humans(index):
        return [
            item.content for item in model.requests[index] if getattr(item, "type", None) == "human"
        ]

    assert humans(1) == ["one", "two"]
    assert humans(2) == ["three"]


def test_configured_agents_compile_into_the_graph(runtime):
    from pydantic import SecretStr, ValidationError

    from dataagent.assembly.graph import load_extensions

    with pytest.raises(ValidationError, match="duplicate ids"):
        A2ASettings(
            remote_agents=(
                A2AAgentSettings(id="researcher", url="http://127.0.0.1:9999"),
                A2AAgentSettings(id="researcher", url="http://127.0.0.1:9998"),
            )
        )
    settings = runtime.settings.model_copy(
        update={
            "a2a": A2ASettings(
                remote_agents=(
                    A2AAgentSettings(
                        id="researcher",
                        url="http://127.0.0.1:9999",
                        description="Ask the research agent.",
                        auth_token=SecretStr("a2a-token"),
                    ),
                )
            )
        }
    )
    prepared = replace(runtime, settings=settings)
    names = [tool.name for tool in load_extensions(prepared)["tools"]]
    assert "a2a__researcher" in names
    assert "a2a-token" in prepared.redaction_secrets


@asynccontextmanager
async def peer(respond):
    """A JSON-RPC peer that returns the protobuf ``SendMessageResponse`` from ``respond``."""
    from a2a.server.request_handlers.response_helpers import agent_card_to_dict
    from google.protobuf.json_format import MessageToDict
    from starlette.applications import Starlette
    from starlette.responses import JSONResponse
    from starlette.routing import Route

    from dataagent.server.a2a.agent_card import build_agent_card

    card = agent_card_to_dict(build_agent_card(host="127.0.0.1", port=9999))

    async def get_card(_request):
        return JSONResponse(card)

    async def rpc(request):
        body = await request.json()
        message = body["params"]["message"]
        result = MessageToDict(respond(message))
        return JSONResponse({"jsonrpc": "2.0", "id": body.get("id"), "result": result})

    app = Starlette(
        routes=[
            Route("/.well-known/agent-card.json", get_card),
            Route("/a2a/jsonrpc", rpc, methods=["POST"]),
        ]
    )
    transport = httpx.ASGITransport(app=app)
    async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:9999") as http:
        yield http


async def test_direct_message_response_keeps_the_context():
    from a2a.helpers import new_text_message
    from a2a.types.a2a_pb2 import Role, SendMessageResponse

    def respond(_message):
        return SendMessageResponse(
            message=new_text_message(
                "plain-answer",
                context_id="ctx-msg",
                role=Role.ROLE_AGENT,
            )
        )

    async with peer(respond) as http:
        result = await A2ACaller("http://127.0.0.1:9999", http_client=http).send("hello")
    assert result.text == "plain-answer"
    assert result.context_id == "ctx-msg"
    assert result.state == "MESSAGE"


async def test_input_required_follow_up_keeps_the_task():
    from a2a.helpers import new_text_artifact, new_text_message
    from a2a.types.a2a_pb2 import SendMessageResponse, Task, TaskState, TaskStatus

    seen = []

    def respond(message):
        seen.append(message.get("taskId") or message.get("task_id"))
        if len(seen) == 1:
            return SendMessageResponse(
                task=Task(
                    id="task-wait",
                    context_id="ctx-1",
                    status=TaskStatus(
                        state=TaskState.TASK_STATE_INPUT_REQUIRED,
                        message=new_text_message(
                            "need more", context_id="ctx-1", task_id="task-wait"
                        ),
                    ),
                )
            )
        return SendMessageResponse(
            task=Task(
                id="task-wait",
                context_id="ctx-1",
                status=TaskStatus(state=TaskState.TASK_STATE_COMPLETED),
                artifacts=[new_text_artifact("dataagent_result", "done")],
            )
        )

    async with peer(respond) as http:
        tool = remote_agent_tool("researcher", "http://127.0.0.1:9999", http_client=http)
        waiting = await tool.ainvoke(
            {"message": "start"},
            config={"configurable": {"thread_id": "local-1"}},
        )
        finished = await tool.ainvoke(
            {"message": "more"},
            config={"configurable": {"thread_id": "local-1"}},
        )
    assert waiting == "INPUT_REQUIRED\nneed more"
    assert seen == [None, "task-wait"]
    assert finished == "done"