"""MCP resources — live data endpoints for dish state."""

import json

from fastmcp import Context


def register(mcp):
    """Register MCP resources on the FastMCP instance."""

    @mcp.resource("birdcage://config")
    async def config_resource(ctx: Context) -> str:
        """Current connection configuration."""
        state = ctx.request_context.lifespan_context
        data = {
            "demo_mode": state.demo_mode,
            "serial_port": state.serial_port,
            "firmware": state.firmware_name,
            "connected": state.connected,
        }
        return json.dumps(data, indent=2)

    @mcp.resource("birdcage://position")
    async def position_resource(ctx: Context) -> str:
        """Live dish position (queries hardware)."""
        state = ctx.request_context.lifespan_context
        if state.device is None:
            return json.dumps({"error": "not connected"})
        pos = state.device.get_position()
        return json.dumps(pos, indent=2)

    @mcp.resource("birdcage://firmware")
    async def firmware_resource(ctx: Context) -> str:
        """Firmware identification string."""
        state = ctx.request_context.lifespan_context
        if state.device is None:
            return json.dumps({"error": "not connected"})
        fw_id = state.device.get_firmware_id()
        return fw_id

    @mcp.resource("birdcage://motor-dynamics")
    async def motor_dynamics_resource(ctx: Context) -> str:
        """Motor velocity and acceleration limits."""
        state = ctx.request_context.lifespan_context
        if state.device is None:
            return json.dumps({"error": "not connected"})
        dynamics = state.device.get_motor_dynamics()
        return json.dumps(dynamics, indent=2)

    @mcp.resource("birdcage://el-limits")
    async def el_limits_resource(ctx: Context) -> str:
        """Elevation angle limits (min, max, home)."""
        state = ctx.request_context.lifespan_context
        if state.device is None:
            return json.dumps({"error": "not connected"})
        limits = state.device.get_el_limits()
        return json.dumps(limits, indent=2)
