"""Tests for system and firmware tools."""

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


@pytest.mark.anyio
async def test_get_firmware_id(mcp_client: Client):
    result = await mcp_client.call_tool("get_firmware_id", {})
    data = parse_result(result)
    assert "02.02.48" in data["raw"]
    assert "TWELINCH" in data["raw"]


@pytest.mark.anyio
async def test_get_motor_dynamics(mcp_client: Client):
    result = await mcp_client.call_tool("get_motor_dynamics", {})
    data = parse_result(result)
    assert data["az_max_vel"] == 65.0
    assert data["el_max_vel"] == 45.0
    assert data["az_accel"] == 400.0
    assert data["el_accel"] == 400.0


@pytest.mark.anyio
async def test_set_max_velocity(mcp_client: Client):
    result = await mcp_client.call_tool(
        "set_max_velocity", {"motor_id": 0, "deg_per_sec": 30.0}
    )
    data = parse_result(result)
    assert data["axis"] == "azimuth"
    assert data["max_velocity"] == 30.0

    # Verify it stuck
    result = await mcp_client.call_tool("get_motor_dynamics", {})
    data = parse_result(result)
    assert data["az_max_vel"] == 30.0


@pytest.mark.anyio
async def test_set_max_velocity_invalid_motor(mcp_client: Client):
    with pytest.raises(ToolError):
        await mcp_client.call_tool(
            "set_max_velocity", {"motor_id": 3, "deg_per_sec": 10.0}
        )


@pytest.mark.anyio
async def test_set_max_acceleration(mcp_client: Client):
    result = await mcp_client.call_tool(
        "set_max_acceleration", {"motor_id": 1, "accel": 200.0}
    )
    data = parse_result(result)
    assert data["axis"] == "elevation"
    assert data["max_acceleration"] == 200.0


@pytest.mark.anyio
async def test_get_motor_life(mcp_client: Client):
    result = await mcp_client.call_tool("get_motor_life", {})
    data = parse_result(result)
    assert "AZ total moves" in data["raw"]


@pytest.mark.anyio
async def test_get_pid_gains(mcp_client: Client):
    result = await mcp_client.call_tool("get_pid_gains", {})
    data = parse_result(result)
    assert data["az"]["kp"] == 600.0
    assert data["az"]["kv"] == 60.0
    assert data["el"]["kp"] == 250.0


@pytest.mark.anyio
async def test_set_pid_gains(mcp_client: Client):
    result = await mcp_client.call_tool(
        "set_pid_gains",
        {"motor_id": 0, "kp": 500.0, "kv": 55.0, "ki": 2.0},
    )
    data = parse_result(result)
    assert data["status"] == "set"
    assert data["kp"] == 500.0


@pytest.mark.anyio
async def test_get_a3981_diag(mcp_client: Client):
    result = await mcp_client.call_tool("get_a3981_diag", {})
    data = parse_result(result)
    assert "OK" in data["raw"]


@pytest.mark.anyio
async def test_get_a3981_modes(mcp_client: Client):
    result = await mcp_client.call_tool("get_a3981_modes", {})
    data = parse_result(result)
    assert "AUTO" in data["step_mode"]


@pytest.mark.anyio
async def test_nvs_dump(mcp_client: Client):
    result = await mcp_client.call_tool("nvs_dump", {})
    data = parse_result(result)
    assert "Disable Tracker" in data["raw"]
    assert "AZ Max Vel" in data["raw"]


@pytest.mark.anyio
async def test_nvs_read(mcp_client: Client):
    result = await mcp_client.call_tool("nvs_read", {"index": 20})
    data = parse_result(result)
    assert data["index"] == 20
    assert "Disable Tracker" in data["raw"]


@pytest.mark.anyio
async def test_nvs_read_missing_index(mcp_client: Client):
    result = await mcp_client.call_tool("nvs_read", {"index": 999})
    data = parse_result(result)
    assert "not found" in data["raw"]
