"""
Signals Gateway Tools
Wraps the SignalsGatewayEngine for server-side event routing.
"""

from typing import Dict, Any, Tuple
from functools import partial
import mcp.types as mcp_types

from ..meta_client import MetaClient
from ..schemas import ToolResult
from ..signals_gateway import get_signals_gateway_engine
from .base import ToolHandler


async def get_gateway_instances(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Get gateway instances."""
    engine = get_signals_gateway_engine()
    engine.client = client
    return await engine.get_gateway_instances(arguments)


async def get_gateway_health(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Check gateway health."""
    engine = get_signals_gateway_engine()
    engine.client = client
    return await engine.get_gateway_health(arguments)


async def get_gateway_diagnostics(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Get gateway diagnostics."""
    engine = get_signals_gateway_engine()
    engine.client = client
    return await engine.get_gateway_diagnostics(arguments)


def get_tools(client: MetaClient) -> Dict[str, Tuple[mcp_types.Tool, ToolHandler]]:
    """Register Signals Gateway tools"""
    return {
        "meta.signals.get_gateways": (
            mcp_types.Tool(
                name="meta.signals.get_gateways",
                description="Get all Signals Gateway instances for a business. View server-side event routing infrastructure. Use for Signals Gateway management.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "business_id": {"type": "string", "description": "Business ID"}
                    },
                    "required": ["business_id"]
                }
            ),
            partial(get_gateway_instances, client)
        ),
        "meta.signals.health": (
            mcp_types.Tool(
                name="meta.signals.health",
                description="Check health status of a Signals Gateway instance. Monitor uptime and connectivity. Use for gateway monitoring.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "gateway_id": {"type": "string", "description": "Gateway instance ID"}
                    },
                    "required": ["gateway_id"]
                }
            ),
            partial(get_gateway_health, client)
        ),
        "meta.signals.diagnostics": (
            mcp_types.Tool(
                name="meta.signals.diagnostics",
                description="Get detailed diagnostics for a Signals Gateway including event counts, latency, and error rates. Use for gateway troubleshooting.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "gateway_id": {"type": "string"},
                        "time_range": {
                            "type": "object",
                            "properties": {
                                "since": {"type": "string"},
                                "until": {"type": "string"}
                            }
                        }
                    },
                    "required": ["gateway_id"]
                }
            ),
            partial(get_gateway_diagnostics, client)
        )
    }
