"""Tests for API endpoints."""

import pytest
from fastapi.testclient import TestClient

from wled_controller.main import app
from wled_controller import __version__

client = TestClient(app)


def test_root_endpoint():
    """Test root endpoint returns the HTML dashboard."""
    response = client.get("/")
    assert response.status_code == 200
    assert "text/html" in response.headers["content-type"]
    assert "LED Grab" in response.text


def test_health_check():
    """Test health check endpoint."""
    response = client.get("/health")
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "healthy"
    assert data["version"] == __version__
    assert "timestamp" in data


def test_version_endpoint():
    """Test version endpoint."""
    response = client.get("/api/v1/version")
    assert response.status_code == 200
    data = response.json()
    assert data["version"] == __version__
    assert "python_version" in data
    assert data["api_version"] == "v1"


def test_get_displays():
    """Test get displays endpoint."""
    response = client.get("/api/v1/config/displays")
    assert response.status_code == 200
    data = response.json()
    assert "displays" in data
    assert "count" in data
    assert isinstance(data["displays"], list)
    assert data["count"] >= 0

    # If displays are found, validate structure
    if data["count"] > 0:
        display = data["displays"][0]
        assert "index" in display
        assert "name" in display
        assert "width" in display
        assert "height" in display
        assert "is_primary" in display


def test_openapi_docs():
    """Test OpenAPI documentation is available."""
    response = client.get("/openapi.json")
    assert response.status_code == 200
    data = response.json()
    assert data["info"]["title"] == "WLED Screen Controller"
    assert data["info"]["version"] == __version__


def test_swagger_ui():
    """Test Swagger UI is available."""
    response = client.get("/docs")
    assert response.status_code == 200
    assert "text/html" in response.headers["content-type"]
