"""Tests for the dashboard API endpoints (/api/* and /dashboard)."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
    from httpx import AsyncClient

# -------------------------------------------------------------------
# /api/savings
# -------------------------------------------------------------------


@pytest.mark.unit
async def test_savings_returns_200(client: AsyncClient) -> None:
    resp = await client.get("/api/savings")
    assert resp.status_code == 200


@pytest.mark.unit
async def test_savings_has_expected_fields(client: AsyncClient) -> None:
    data = (await client.get("/api/savings")).json()
    assert "total_requests" in data
    assert "savings_amount" in data
    assert "savings_percent" in data
    assert "period" in data


@pytest.mark.unit
async def test_savings_default_period_is_month(client: AsyncClient) -> None:
    data = (await client.get("/api/savings")).json()
    assert data["period"] == "month"


@pytest.mark.unit
async def test_savings_accepts_period_param(client: AsyncClient) -> None:
    for period in ("day", "week", "month", "all"):
        resp = await client.get(f"/api/savings?period={period}")
        assert resp.status_code == 200
        assert resp.json()["period"] == period


@pytest.mark.unit
async def test_savings_invalid_period_returns_422(client: AsyncClient) -> None:
    resp = await client.get("/api/savings?period=invalid")
    assert resp.status_code == 422


# -------------------------------------------------------------------
# /api/metrics
# -------------------------------------------------------------------


@pytest.mark.unit
async def test_metrics_returns_200(client: AsyncClient) -> None:
    resp = await client.get("/api/metrics")
    assert resp.status_code == 200


@pytest.mark.unit
async def test_metrics_has_expected_fields(client: AsyncClient) -> None:
    data = (await client.get("/api/metrics")).json()
    assert "uptime" in data
    assert "requests_total" in data
    assert "rpm" in data
    assert "cache_hit_rate" in data
    assert "avg_latency" in data


@pytest.mark.unit
async def test_metrics_defaults_are_zero(client: AsyncClient) -> None:
    """Without a metrics collector, all values should be defaults (0)."""
    data = (await client.get("/api/metrics")).json()
    assert data["requests_total"] == 0
    assert data["rpm"] == 0.0


# -------------------------------------------------------------------
# /api/requests
# -------------------------------------------------------------------


@pytest.mark.unit
async def test_requests_returns_200(client: AsyncClient) -> None:
    resp = await client.get("/api/requests")
    assert resp.status_code == 200


@pytest.mark.unit
async def test_requests_has_pagination_fields(client: AsyncClient) -> None:
    data = (await client.get("/api/requests")).json()
    assert "items" in data
    assert "total" in data
    assert "offset" in data
    assert "limit" in data
    assert isinstance(data["items"], list)


@pytest.mark.unit
async def test_requests_empty_when_no_tracker(client: AsyncClient) -> None:
    data = (await client.get("/api/requests")).json()
    assert data["items"] == []
    assert data["total"] == 0


# -------------------------------------------------------------------
# /api/backends
# -------------------------------------------------------------------


@pytest.mark.unit
async def test_backends_returns_200(client: AsyncClient) -> None:
    resp = await client.get("/api/backends")
    assert resp.status_code == 200


@pytest.mark.unit
async def test_backends_has_backends_list(client: AsyncClient) -> None:
    data = (await client.get("/api/backends")).json()
    assert "backends" in data
    assert isinstance(data["backends"], list)


# -------------------------------------------------------------------
# /dashboard (HTML)
# -------------------------------------------------------------------


@pytest.mark.unit
async def test_dashboard_returns_html(client: AsyncClient) -> None:
    resp = await client.get("/dashboard")
    assert resp.status_code == 200
    assert "text/html" in resp.headers.get("content-type", "")
