"""Tests for satellite tracking tools."""

import pytest
from conftest import parse_result
from fastmcp import Client


@pytest.mark.anyio
async def test_search_satellites(mcp_client: Client):
    result = await mcp_client.call_tool(
        "search_satellites", {"query": "ISS"}
    )
    data = parse_result(result)
    assert data["count"] >= 1
    names = [r["name"] for r in data["results"]]
    assert any("ISS" in n for n in names)


@pytest.mark.anyio
async def test_search_no_results(mcp_client: Client):
    result = await mcp_client.call_tool(
        "search_satellites", {"query": "nonexistent_xyz"}
    )
    data = parse_result(result)
    assert data["count"] == 0


@pytest.mark.anyio
async def test_search_with_limit(mcp_client: Client):
    result = await mcp_client.call_tool(
        "search_satellites", {"query": "", "limit": 3}
    )
    data = parse_result(result)
    # Empty query won't match any catalog entries
    assert data["count"] >= 0


@pytest.mark.anyio
async def test_get_passes(mcp_client: Client):
    result = await mcp_client.call_tool(
        "get_passes", {"norad_id": 25544}
    )
    data = parse_result(result)
    assert data["count"] > 0
    p = data["passes"][0]
    assert "aos_time" in p
    assert "tca_time" in p
    assert "los_time" in p
    assert "max_elevation" in p
    assert p["norad_id"] == 25544


@pytest.mark.anyio
async def test_get_next_pass(mcp_client: Client):
    result = await mcp_client.call_tool(
        "get_next_pass", {"norad_id": 25544}
    )
    data = parse_result(result)
    assert data["pass"] is not None
    assert data["pass"]["satellite_name"] == "ISS (ZARYA)"


@pytest.mark.anyio
async def test_get_visible_targets(mcp_client: Client):
    result = await mcp_client.call_tool(
        "get_visible_targets", {}
    )
    data = parse_result(result)
    # Celestial bodies (Moon, Sun, Jupiter) are always visible
    assert data["count"] >= 1
    names = [t["name"] for t in data["targets"]]
    # At least celestial bodies should be present
    assert any(
        n in names for n in ("Moon", "Sun", "Jupiter")
    )


@pytest.mark.anyio
async def test_get_visible_targets_with_min_alt(mcp_client: Client):
    result = await mcp_client.call_tool(
        "get_visible_targets", {"min_alt": 80.0}
    )
    data = parse_result(result)
    # Very high min_alt should filter out most targets
    for t in data["targets"]:
        assert t["altitude"] >= 80.0
