"""Support for AVM FRITZ!Box update platform."""

from dataclasses import dataclass
from typing import Any, override

from homeassistant.components.update import (
    UpdateEntity,
    UpdateEntityDescription,
    UpdateEntityFeature,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .const import LOGGER
from .coordinator import AvmWrapper, FritzConfigEntry
from .entity import FritzBoxBaseCoordinatorEntity, FritzEntityDescription

# Set a sane value to avoid too many updates
PARALLEL_UPDATES = 5


@dataclass(frozen=True, kw_only=True)
class FritzUpdateEntityDescription(UpdateEntityDescription, FritzEntityDescription):
    """Describes Fritz update entity."""


async def async_setup_entry(
    hass: HomeAssistant,
    entry: FritzConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up AVM FRITZ!Box update entities."""
    LOGGER.debug("Setting up AVM FRITZ!Box update entities")
    avm_wrapper = entry.runtime_data

    entities = [FritzBoxUpdateEntity(avm_wrapper, entry.title)]

    async_add_entities(entities)


class FritzBoxUpdateEntity(FritzBoxBaseCoordinatorEntity, UpdateEntity):
    """Mixin for update entity specific attributes."""

    _attr_entity_category = EntityCategory.CONFIG
    _attr_supported_features = UpdateEntityFeature.INSTALL
    _attr_title = "FRITZ!OS"
    entity_description: FritzUpdateEntityDescription

    def __init__(
        self,
        avm_wrapper: AvmWrapper,
        device_friendly_name: str,
    ) -> None:
        """Init FRITZ!Box connectivity class."""
        description = FritzUpdateEntityDescription(
            key="update", name="FRITZ!OS", value_fn=None
        )
        super().__init__(avm_wrapper, device_friendly_name, description)

    @property
    @override
    def installed_version(self) -> str | None:
        """Version currently in use."""
        return self.coordinator.current_firmware

    @property
    @override
    def latest_version(self) -> str | None:
        """Latest version available for install."""
        if self.coordinator.update_available:
            return self.coordinator.latest_firmware
        return self.coordinator.current_firmware

    @property
    @override
    def release_url(self) -> str | None:
        """URL to the full release notes of the latest version available."""
        return self.coordinator.release_url

    @override
    async def async_install(
        self, version: str | None, backup: bool, **kwargs: Any
    ) -> None:
        """Install an update."""
        await self.coordinator.async_trigger_firmware_update()
