"""MCP gateway — register and proxy external MCP servers."""

from __future__ import annotations

from typing import Any, Dict, Optional

from .._resource import AsyncResource, Resource


class MCP(Resource):
    """Sync MCP gateway operations."""

    def list_servers(self) -> Dict[str, Any]:
        """List registered MCP servers."""
        return self._get("/v1/mcp/servers")

    def get_server(self, server_id: str) -> Dict[str, Any]:
        """Get an MCP server by ID."""
        return self._get(f"/v1/mcp/servers/{server_id}")

    def register_server(self, params: Dict[str, Any]) -> Dict[str, Any]:
        """Register a new MCP server."""
        return self._post("/v1/mcp/servers", params)

    def update_server(self, server_id: str, params: Dict[str, Any]) -> Dict[str, Any]:
        """Update an MCP server."""
        return self._put(f"/v1/mcp/servers/{server_id}", params)

    def remove_server(self, server_id: str) -> Dict[str, Any]:
        """Remove an MCP server."""
        return self._delete(f"/v1/mcp/servers/{server_id}")

    def list_tools(self, server_id: Optional[str] = None) -> Dict[str, Any]:
        """List available MCP tools (optionally filtered by server)."""
        query = {"server_id": server_id} if server_id else None
        return self._get("/v1/mcp/tools", query)

    def call_tool(
        self,
        server_id: str,
        tool: str,
        *,
        arguments: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """Call a tool on an MCP server."""
        body: Dict[str, Any] = {"server_id": server_id, "tool": tool}
        if arguments is not None:
            body["arguments"] = arguments
        return self._post("/v1/mcp/tools/call", body)

    def audit(self, *, limit: Optional[int] = None) -> Dict[str, Any]:
        """Get MCP tool call audit trail."""
        query = {"limit": str(limit)} if limit else None
        return self._get("/v1/mcp/audit", query)


class AsyncMCP(AsyncResource):
    """Async MCP gateway operations."""

    async def list_servers(self) -> Dict[str, Any]:
        return await self._get("/v1/mcp/servers")

    async def get_server(self, server_id: str) -> Dict[str, Any]:
        return await self._get(f"/v1/mcp/servers/{server_id}")

    async def register_server(self, params: Dict[str, Any]) -> Dict[str, Any]:
        return await self._post("/v1/mcp/servers", params)

    async def update_server(self, server_id: str, params: Dict[str, Any]) -> Dict[str, Any]:
        return await self._put(f"/v1/mcp/servers/{server_id}", params)

    async def remove_server(self, server_id: str) -> Dict[str, Any]:
        return await self._delete(f"/v1/mcp/servers/{server_id}")

    async def list_tools(self, server_id: Optional[str] = None) -> Dict[str, Any]:
        query = {"server_id": server_id} if server_id else None
        return await self._get("/v1/mcp/tools", query)

    async def call_tool(
        self,
        server_id: str,
        tool: str,
        *,
        arguments: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        body: Dict[str, Any] = {"server_id": server_id, "tool": tool}
        if arguments is not None:
            body["arguments"] = arguments
        return await self._post("/v1/mcp/tools/call", body)

    async def audit(self, *, limit: Optional[int] = None) -> Dict[str, Any]:
        query = {"limit": str(limit)} if limit else None
        return await self._get("/v1/mcp/audit", query)
