"""Tests for signal measurement tools."""

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


@pytest.mark.anyio
async def test_get_rssi_default(mcp_client: Client):
    result = await mcp_client.call_tool("get_rssi", {})
    data = parse_result(result)
    assert data["reads"] == 10
    assert isinstance(data["average"], int)
    assert isinstance(data["current"], int)


@pytest.mark.anyio
async def test_get_rssi_custom_iterations(mcp_client: Client):
    result = await mcp_client.call_tool("get_rssi", {"iterations": 5})
    data = parse_result(result)
    assert data["reads"] == 5


@pytest.mark.anyio
async def test_get_adc_rssi(mcp_client: Client):
    result = await mcp_client.call_tool("get_adc_rssi", {})
    data = parse_result(result)
    assert "raw" in data


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


@pytest.mark.anyio
async def test_enable_lna(mcp_client: Client):
    result = await mcp_client.call_tool("enable_lna", {})
    data = parse_result(result)
    assert data["status"] == "lna_enabled"
    assert data["voltage"] == "13V"
    assert data["polarity"] == "V"


@pytest.mark.anyio
async def test_set_lnb_voltage_odu(mcp_client: Client):
    result = await mcp_client.call_tool(
        "set_lnb_voltage", {"mode": "odu"}
    )
    data = parse_result(result)
    assert data["voltage"] == "13V"
    assert data["polarity"] == "V"
    assert data["mode"] == "odu"


@pytest.mark.anyio
async def test_set_lnb_voltage_stb(mcp_client: Client):
    result = await mcp_client.call_tool(
        "set_lnb_voltage", {"mode": "stb"}
    )
    data = parse_result(result)
    assert data["voltage"] == "18V"
    assert data["polarity"] == "H"
    assert data["mode"] == "stb"


@pytest.mark.anyio
async def test_set_lnb_voltage_invalid(mcp_client: Client):
    with pytest.raises(ToolError):
        await mcp_client.call_tool(
            "set_lnb_voltage", {"mode": "invalid"}
        )


@pytest.mark.anyio
async def test_get_dvb_config(mcp_client: Client):
    result = await mcp_client.call_tool("get_dvb_config", {})
    data = parse_result(result)
    assert "BCM" in data["raw"]
    assert "0x4515" in data["raw"]


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


@pytest.mark.anyio
async def test_az_sweep(mcp_client: Client):
    result = await mcp_client.call_tool(
        "az_sweep",
        {
            "start_az": 195.0,
            "span": 10.0,
            "step_cdeg": 200,
            "num_xponders": 1,
        },
    )
    data = parse_result(result)
    assert data["count"] > 0
    assert len(data["points"]) == data["count"]
    pt = data["points"][0]
    assert "az" in pt
    assert "rssi" in pt
    assert "lock" in pt
    assert "snr" in pt
