"""MQTT device provider — factory, validation, health checks."""

from typing import List

from wled_controller.core.devices.led_client import (
    DeviceHealth,
    DiscoveredDevice,
    LEDClient,
    LEDDeviceProvider,
)
from wled_controller.core.devices.mqtt_client import (
    MQTTLEDClient,
    parse_mqtt_url,
)
from wled_controller.utils import get_logger

logger = get_logger(__name__)


class MQTTDeviceProvider(LEDDeviceProvider):
    """Provider for MQTT-based LED devices."""

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

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

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

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

    async def validate_device(self, url: str) -> dict:
        """Validate MQTT device URL (topic path)."""
        topic = parse_mqtt_url(url)
        if not topic or topic == "/":
            raise ValueError("MQTT topic cannot be empty")
        # Can't auto-detect LED count — require manual entry
        return {}

    async def discover(self, timeout: float = 3.0) -> List[DiscoveredDevice]:
        # MQTT devices are not auto-discoverable
        return []
