"""Tests for MCP remote config emitters."""

from __future__ import annotations

import pytest

from aria_agentkit.mcp.claude_desktop import claude_desktop_config
from aria_agentkit.mcp.sdk_session import mcp_session_params, mcp_session_params_factory


class TestClaudeDesktopConfig:
    def test_basic_config(self) -> None:
        cfg = claude_desktop_config(
            server_name="aria",
            gateway_url="https://mcp.acme.com/v1/mcp",
            token="tok123",
        )
        assert "aria" in cfg
        server = cfg["aria"]
        assert server["url"] == "https://mcp.acme.com/v1/mcp"
        assert server["transport"] == "streamable-http"
        assert server["headers"]["Authorization"] == "Bearer tok123"
        assert "Mcp-Protocol-Version" in server["headers"]

    def test_custom_protocol_version(self) -> None:
        cfg = claude_desktop_config(
            server_name="aria",
            gateway_url="https://mcp.example.com/v1/mcp",
            token="tok",
            protocol_version="2024-11-05",
        )
        assert cfg["aria"]["headers"]["Mcp-Protocol-Version"] == "2024-11-05"

    def test_empty_gateway_url_raises(self) -> None:
        with pytest.raises(ValueError, match="gateway_url"):
            claude_desktop_config(server_name="s", gateway_url="", token="t")

    def test_empty_token_raises(self) -> None:
        with pytest.raises(ValueError, match="token"):
            claude_desktop_config(server_name="s", gateway_url="https://x", token="")

    def test_empty_server_name_raises(self) -> None:
        with pytest.raises(ValueError, match="server_name"):
            claude_desktop_config(server_name="", gateway_url="https://x", token="t")


class TestMcpSessionParams:
    def test_basic_params(self) -> None:
        params = mcp_session_params(
            gateway_url="https://mcp.acme.com/v1/mcp",
            token="my-token",
        )
        assert params["url"] == "https://mcp.acme.com/v1/mcp"
        assert params["transport"] == "streamable-http"
        assert params["headers"]["Authorization"] == "Bearer my-token"
        assert params["headers"]["Mcp-Protocol-Version"] == "2025-03-26"
        assert params["headers"]["Origin"] == "https://mcp.acme.com"

    def test_custom_origin(self) -> None:
        params = mcp_session_params(
            gateway_url="https://gw.example.com/v1/mcp",
            token="tok",
            origin="https://my-app.example.com",
        )
        assert params["headers"]["Origin"] == "https://my-app.example.com"

    def test_origin_auto_extracted(self) -> None:
        params = mcp_session_params(
            gateway_url="https://gateway.acme.com:8443/v1/mcp",
            token="tok",
        )
        assert params["headers"]["Origin"] == "https://gateway.acme.com:8443"

    def test_empty_url_raises(self) -> None:
        with pytest.raises(ValueError, match="gateway_url"):
            mcp_session_params(gateway_url="", token="tok")

    def test_empty_token_raises(self) -> None:
        with pytest.raises(ValueError, match="token"):
            mcp_session_params(gateway_url="https://x", token="")


class TestMcpSessionParamsFactory:
    def test_returns_callable(self) -> None:
        factory = mcp_session_params_factory(
            gateway_url="https://mcp.acme.com/v1/mcp",
            token_factory=lambda: "tok",
        )
        assert callable(factory)

    def test_factory_calls_token_factory(self) -> None:
        call_count = 0

        def counter() -> str:
            nonlocal call_count
            call_count += 1
            return f"token-{call_count}"

        factory = mcp_session_params_factory(
            gateway_url="https://mcp.acme.com/v1/mcp",
            token_factory=counter,
        )
        p1 = factory()
        p2 = factory()
        assert p1["headers"]["Authorization"] == "Bearer token-1"
        assert p2["headers"]["Authorization"] == "Bearer token-2"
        assert call_count == 2

    def test_factory_empty_url_raises(self) -> None:
        with pytest.raises(ValueError, match="gateway_url"):
            mcp_session_params_factory(gateway_url="", token_factory=lambda: "tok")

    def test_factory_none_token_factory_raises(self) -> None:
        with pytest.raises(ValueError, match="token_factory"):
            mcp_session_params_factory(
                gateway_url="https://x",
                token_factory=None,  # type: ignore[arg-type]
            )

    def test_factory_params_structure(self) -> None:
        factory = mcp_session_params_factory(
            gateway_url="https://mcp.acme.com/v1/mcp",
            token_factory=lambda: "tok",
        )
        params = factory()
        assert params["url"] == "https://mcp.acme.com/v1/mcp"
        assert params["transport"] == "streamable-http"
        assert "Origin" in params["headers"]
        assert "Mcp-Protocol-Version" in params["headers"]
