import httpx
import pytest

from mistralai.workflows._version import __version__
from mistralai.workflows.client import get_async_client
from mistralai.workflows.core.worker_client import WorkerClient


class TestAsyncClientUserAgent:
    def test_default_user_agent_is_sent(self):
        client = get_async_client(api_key="test-key")
        assert client.headers["user-agent"] == f"mistral-client-python/workflows-worker/{__version__}"

    def test_custom_user_agent_is_preserved(self):
        client = get_async_client(api_key="test-key", headers={"User-Agent": "custom-agent/1.0"})
        assert client.headers["user-agent"] == "custom-agent/1.0"

    def test_custom_user_agent_case_insensitive(self):
        client = get_async_client(api_key="test-key", headers={"user-agent": "custom-agent/2.0"})
        assert client.headers["user-agent"] == "custom-agent/2.0"


class TestSdkClientUserAgent:
    @pytest.mark.asyncio
    async def test_sdk_client_appends_mistral_user_agent(self, monkeypatch):
        """Verify SDK requests include the Mistral user-agent appended to the Speakeasy one."""
        captured_headers = {}

        async def capture_request(request: httpx.Request) -> httpx.Response:
            captured_headers.update(request.headers)
            return httpx.Response(
                200,
                json={
                    "worker_id": "x",
                    "executor_id": "y",
                    "deployment_name": "z",
                    "scheduler_url": "http://scheduler",
                    "namespace": "ns",
                },
            )

        client = WorkerClient(base_url="http://localhost", api_key="test-key")
        monkeypatch.setattr(client._async_client, "_transport", httpx.MockTransport(capture_request))

        async with client:
            await client.who_am_i()

        ua = captured_headers.get("user-agent", "")
        assert f"mistral-client-python/workflows-worker/{__version__}" in ua
        assert "speakeasy-sdk" in ua

    @pytest.mark.asyncio
    async def test_sdk_client_mistral_user_agent_is_last_token(self, monkeypatch):
        """Verify the Mistral UA is appended after the Speakeasy one."""
        captured_headers = {}

        async def capture_request(request: httpx.Request) -> httpx.Response:
            captured_headers.update(request.headers)
            return httpx.Response(
                200,
                json={
                    "worker_id": "x",
                    "executor_id": "y",
                    "deployment_name": "z",
                    "scheduler_url": "http://scheduler",
                    "namespace": "ns",
                },
            )

        client = WorkerClient(base_url="http://localhost", api_key="test-key")
        monkeypatch.setattr(client._async_client, "_transport", httpx.MockTransport(capture_request))

        async with client:
            await client.who_am_i()

        ua = captured_headers.get("user-agent", "")
        mistral_part = f"mistral-client-python/workflows-worker/{__version__}"
        assert ua.endswith(mistral_part), f"Mistral UA should be last in: {ua!r}"
