"""
Simple echo MCP server for integration testing.

This is a minimal MCP server that provides basic tools for testing
client-server communication without requiring the full LDR stack.
"""

import sys
from typing import Any, Dict

from loguru import logger

# Configure loguru to output to stderr to avoid stdout pollution
# (MCP uses stdout for JSON-RPC communication)
logger.remove()  # Remove default handler
logger.add(
    sys.stderr,
    level="INFO",
    format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {message}",
)


try:
    from mcp.server.fastmcp import FastMCP

    MCP_AVAILABLE = True
except ImportError:
    MCP_AVAILABLE = False
    logger.warning("MCP package not available")


if MCP_AVAILABLE:
    # Create FastMCP server instance
    mcp = FastMCP(
        "echo-test-server",
        instructions="Simple echo server for testing MCP client-server communication",
    )

    @mcp.tool()
    def echo(message: str) -> Dict[str, Any]:
        """
        Echo back the provided message.

        Args:
            message: The message to echo back.

        Returns:
            Dictionary with the echoed message.
        """
        logger.info(f"Echo tool called with: {message}")
        return {
            "status": "success",
            "message": message,
            "echoed": True,
        }

    @mcp.tool()
    def add_numbers(a: int, b: int) -> Dict[str, Any]:
        """
        Add two numbers together.

        Args:
            a: First number.
            b: Second number.

        Returns:
            Dictionary with the sum.
        """
        logger.info(f"Add tool called with: {a} + {b}")
        return {
            "status": "success",
            "result": a + b,
        }

    @mcp.tool()
    def get_info() -> Dict[str, Any]:
        """
        Get server information.

        Returns:
            Dictionary with server details.
        """
        return {
            "status": "success",
            "server_name": "echo-test-server",
            "tools": [
                "echo",
                "add_numbers",
                "get_info",
                "slow_operation",
                "fail_on_demand",
            ],
        }

    @mcp.tool()
    def slow_operation(delay_ms: int = 100) -> Dict[str, Any]:
        """
        Simulate a slow operation for timeout testing.

        Args:
            delay_ms: Delay in milliseconds (capped at 5000ms for safety).

        Returns:
            Dictionary confirming completion.
        """
        import time

        # Cap delay to 5 seconds for safety
        actual_delay = min(delay_ms, 5000) / 1000.0
        logger.info(f"Slow operation sleeping for {actual_delay}s")
        time.sleep(actual_delay)
        return {
            "status": "success",
            "slept_ms": int(actual_delay * 1000),
        }

    @mcp.tool()
    def fail_on_demand(
        should_fail: bool = False, error_message: str = "Intentional failure"
    ) -> Dict[str, Any]:
        """
        A tool that can be made to fail for error handling tests.

        Args:
            should_fail: Whether to raise an exception.
            error_message: The error message to use if failing.

        Returns:
            Success dict or raises exception.
        """
        if should_fail:
            raise ValueError(error_message)
        return {
            "status": "success",
            "failed": False,
        }

    def run_server():
        """Run the echo MCP server using STDIO transport."""
        logger.info("Starting echo test MCP server...")
        mcp.run(transport="stdio")


if __name__ == "__main__":
    if not MCP_AVAILABLE:
        print("MCP package not available", file=sys.stderr)
        sys.exit(1)
    run_server()
