"""SteelSeries GameSense device provider — control SteelSeries RGB peripherals."""

from typing import List

from wled_controller.core.devices.led_client import (
    DeviceHealth,
    DiscoveredDevice,
    LEDClient,
    LEDDeviceProvider,
)
from wled_controller.core.devices.gamesense_client import (
    GameSenseClient,
    GAMESENSE_ZONES,
    _get_gamesense_address,
)
from wled_controller.utils import get_logger

logger = get_logger(__name__)


class GameSenseDeviceProvider(LEDDeviceProvider):
    """Provider for SteelSeries GameSense RGB peripherals.

    URL format: gamesense://address or gamesense://auto (auto-discover from coreProps.json)
    Requires SteelSeries GG / SteelSeries Engine 3.
    """

    @property
    def device_type(self) -> str:
        return "gamesense"

    @property
    def capabilities(self) -> set:
        return {"manual_led_count", "health_check"}

    def create_client(self, url: str, **kwargs) -> LEDClient:
        return GameSenseClient(
            url=url,
            led_count=kwargs.get("led_count", 0),
            gamesense_device_type=kwargs.get("gamesense_device_type", "keyboard"),
        )

    async def check_health(self, url: str, http_client, prev_health=None) -> DeviceHealth:
        return await GameSenseClient.check_health(url, http_client, prev_health)

    async def validate_device(self, url: str) -> dict:
        """Validate GameSense is reachable."""
        address = url.replace("gamesense://", "").strip()
        if not address or address == "auto":
            address = _get_gamesense_address()

        if not address:
            raise ValueError(
                "Cannot find SteelSeries Engine. "
                "Ensure SteelSeries GG is running."
            )

        import httpx

        try:
            async with httpx.AsyncClient(timeout=3.0) as client:
                resp = await client.get(f"http://{address}")
                if resp.status_code >= 500:
                    raise ValueError("SteelSeries Engine returned server error")
        except httpx.ConnectError:
            raise ValueError(
                "Cannot connect to SteelSeries Engine at "
                f"{address}. Ensure it is running."
            )

        return {}

    async def discover(self, timeout: float = 3.0) -> List[DiscoveredDevice]:
        """Discover SteelSeries Engine if running."""
        address = _get_gamesense_address()
        if not address:
            return []

        import httpx

        try:
            async with httpx.AsyncClient(timeout=timeout) as client:
                resp = await client.get(f"http://{address}")
                if resp.status_code >= 500:
                    return []
        except Exception:
            return []

        # Engine is running — offer device types
        results = []
        for dev_type in GAMESENSE_ZONES:
            results.append(
                DiscoveredDevice(
                    name=f"SteelSeries {dev_type.title()}",
                    url=f"gamesense://{address}",
                    device_type="gamesense",
                    ip=address.split(":")[0],
                    mac="",
                    led_count=None,
                    version=None,
                )
            )
        return results
