"""The pi_hole component."""

from typing import override

from hole import Hole

from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import PiHoleUpdateCoordinator


class PiHoleEntity(CoordinatorEntity[PiHoleUpdateCoordinator]):
    """Representation of a Pi-hole entity."""

    def __init__(
        self,
        api: Hole,
        coordinator: PiHoleUpdateCoordinator,
        name: str,
        server_unique_id: str,
    ) -> None:
        """Initialize a Pi-hole entity."""
        super().__init__(coordinator)
        self.api = api
        self._name = name
        self._server_unique_id = server_unique_id

    @property
    @override
    def device_info(self) -> DeviceInfo:
        """Return the device information of the entity."""
        if (
            getattr(self.api, "tls", None)  # API version 5
            or getattr(self.api, "protocol", None) == "https"  # API version 6
        ):
            config_url = f"https://{self.api.host}/{self.api.location}"
        else:
            config_url = f"http://{self.api.host}/{self.api.location}"

        return DeviceInfo(
            identifiers={(DOMAIN, self._server_unique_id)},
            name=self._name,
            manufacturer="Pi-hole",
            configuration_url=config_url,
        )
