"""Support for tariff selection."""

import logging
from typing import override

from homeassistant.components.select import DOMAIN as SELECT_DOMAIN, SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device import async_entity_id_to_device
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.entity_platform import (
    AddConfigEntryEntitiesCallback,
    AddEntitiesCallback,
    async_create_platform_config_not_supported_issue,
)
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from .const import CONF_METER, CONF_SOURCE_SENSOR, CONF_TARIFFS, DATA_UTILITY, DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Initialize Utility Meter config entry."""
    name = config_entry.title
    tariffs: list[str] = config_entry.options[CONF_TARIFFS]

    unique_id = config_entry.entry_id

    device = async_entity_id_to_device(
        hass,
        config_entry.options[CONF_SOURCE_SENSOR],
    )

    tariff_select = TariffSelect(
        name=name,
        tariffs=tariffs,
        unique_id=unique_id,
        device=device,
    )
    async_add_entities([tariff_select])


async def async_setup_platform(
    hass: HomeAssistant,
    conf: ConfigType,
    async_add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the utility meter select."""
    if discovery_info is None:
        async_create_platform_config_not_supported_issue(
            hass,
            DOMAIN,
            SELECT_DOMAIN,
            yaml_config_under_integration_supported=True,
            learn_more_url="https://www.home-assistant.io/integrations/utility_meter/",
            logger=_LOGGER,
        )
        return

    meter: str = discovery_info[CONF_METER]
    meter_info = hass.data[DATA_UTILITY][meter]
    conf_meter_unique_id = meter_info.get(CONF_UNIQUE_ID)
    conf_meter_name = meter_info.get(CONF_NAME, meter)

    async_add_entities(
        [
            TariffSelect(
                name=conf_meter_name,
                tariffs=discovery_info[CONF_TARIFFS],
                yaml_slug=meter,
                unique_id=conf_meter_unique_id,
            )
        ]
    )


class TariffSelect(SelectEntity, RestoreEntity):
    """Representation of a Tariff selector."""

    _attr_translation_key = "tariff"

    def __init__(
        self,
        name,
        tariffs: list[str],
        *,
        yaml_slug: str | None = None,
        unique_id: str | None = None,
        device: DeviceEntry | None = None,
    ) -> None:
        """Initialize a tariff selector."""
        self._attr_name = name
        if yaml_slug:  # Backwards compatibility with YAML configuration entries
            self.entity_id = f"select.{yaml_slug}"
        self._attr_unique_id = unique_id
        self.device_entry = device
        self._current_tariff: str | None = None
        self._tariffs = tariffs
        self._attr_should_poll = False

    @property
    @override
    def options(self) -> list[str]:
        """Return the available tariffs."""
        return self._tariffs

    @property
    @override
    def current_option(self) -> str | None:
        """Return current tariff."""
        return self._current_tariff

    @override
    async def async_added_to_hass(self) -> None:
        """Run when entity about to be added."""
        await super().async_added_to_hass()

        state = await self.async_get_last_state()
        if not state or state.state not in self._tariffs:
            self._current_tariff = self._tariffs[0]
        else:
            self._current_tariff = state.state

    @override
    async def async_select_option(self, option: str) -> None:
        """Select new tariff (option)."""
        self._current_tariff = option
        self.async_write_ha_state()
