"""Tests for movement tools."""

import pytest
from conftest import parse_result
from fastmcp import Client
from fastmcp.exceptions import ToolError


@pytest.mark.anyio
async def test_get_position(mcp_client: Client):
    result = await mcp_client.call_tool("get_position", {})
    data = parse_result(result)
    assert "azimuth" in data
    assert "elevation" in data
    assert isinstance(data["azimuth"], float)
    assert isinstance(data["elevation"], float)


@pytest.mark.anyio
async def test_move_to(mcp_client: Client):
    result = await mcp_client.call_tool(
        "move_to", {"azimuth": 200.0, "elevation": 40.0}
    )
    data = parse_result(result)
    assert data["status"] == "moving"
    assert data["target_azimuth"] == 200.0
    assert data["target_elevation"] == 40.0


@pytest.mark.anyio
async def test_move_motor_az(mcp_client: Client):
    result = await mcp_client.call_tool(
        "move_motor", {"motor_id": 0, "degrees": 90.0}
    )
    data = parse_result(result)
    assert data["axis"] == "azimuth"
    assert data["target"] == 90.0


@pytest.mark.anyio
async def test_move_motor_el(mcp_client: Client):
    result = await mcp_client.call_tool(
        "move_motor", {"motor_id": 1, "degrees": 30.0}
    )
    data = parse_result(result)
    assert data["axis"] == "elevation"


@pytest.mark.anyio
async def test_move_motor_invalid_id(mcp_client: Client):
    with pytest.raises(ToolError):
        await mcp_client.call_tool(
            "move_motor", {"motor_id": 2, "degrees": 10.0}
        )


@pytest.mark.anyio
async def test_home_motor(mcp_client: Client):
    result = await mcp_client.call_tool("home_motor", {"motor_id": 1})
    data = parse_result(result)
    assert data["status"] == "homing"
    assert data["axis"] == "elevation"


@pytest.mark.anyio
async def test_engage_release(mcp_client: Client):
    result = await mcp_client.call_tool("release_motors", {})
    data = parse_result(result)
    assert data["status"] == "released"

    result = await mcp_client.call_tool("engage_motors", {})
    data = parse_result(result)
    assert data["status"] == "engaged"


@pytest.mark.anyio
async def test_stow(mcp_client: Client):
    result = await mcp_client.call_tool("stow", {})
    data = parse_result(result)
    assert data["status"] == "stowing"
    assert data["target_azimuth"] == 0.0
    assert data["target_elevation"] == 65.0


@pytest.mark.anyio
async def test_get_step_positions(mcp_client: Client):
    result = await mcp_client.call_tool("get_step_positions", {})
    data = parse_result(result)
    assert "az_steps" in data
    assert "el_steps" in data
    assert isinstance(data["az_steps"], int)


@pytest.mark.anyio
async def test_get_el_limits(mcp_client: Client):
    result = await mcp_client.call_tool("get_el_limits", {})
    data = parse_result(result)
    assert data["min"] == 18.0
    assert data["max"] == 65.0
    assert data["home"] == 65.0
