"""The ntfy integration."""

import logging

from aiontfy import Ntfy
from aiontfy.exceptions import (
    NtfyConnectionError,
    NtfyHTTPError,
    NtfyTimeoutError,
    NtfyUnauthorizedAuthenticationError,
)
from aiontfy.update import UpdateChecker

from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.hass_dict import HassKey

from .const import DOMAIN
from .coordinator import (
    NtfyConfigEntry,
    NtfyDataUpdateCoordinator,
    NtfyLatestReleaseUpdateCoordinator,
    NtfyRuntimeData,
    NtfyVersionDataUpdateCoordinator,
)
from .services import async_setup_services

_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [
    Platform.EVENT,
    Platform.NOTIFY,
    Platform.SENSOR,
    Platform.UPDATE,
]
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
NTFY_KEY: HassKey[NtfyLatestReleaseUpdateCoordinator] = HassKey(DOMAIN)


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
    """Set up the ntfy services."""

    async_setup_services(hass)
    return True


async def async_setup_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool:
    """Set up ntfy from a config entry."""

    session = async_get_clientsession(hass, entry.data.get(CONF_VERIFY_SSL, True))
    ntfy = Ntfy(entry.data[CONF_URL], session, token=entry.data.get(CONF_TOKEN))
    if NTFY_KEY not in hass.data:
        update_checker = UpdateChecker(session)
        update_coordinator = NtfyLatestReleaseUpdateCoordinator(hass, update_checker)
        await update_coordinator.async_request_refresh()
        hass.data[NTFY_KEY] = update_coordinator

    try:
        await ntfy.account()
    except NtfyUnauthorizedAuthenticationError as e:
        raise ConfigEntryAuthFailed(
            translation_domain=DOMAIN,
            translation_key="authentication_error",
        ) from e
    except NtfyHTTPError as e:
        _LOGGER.debug("Error %s: %s [%s]", e.code, e.error, e.link)
        raise ConfigEntryNotReady(
            translation_domain=DOMAIN,
            translation_key="server_error",
            translation_placeholders={"error_msg": str(e.error)},
        ) from e
    except NtfyConnectionError as e:
        _LOGGER.debug("Error", exc_info=True)
        raise ConfigEntryNotReady(
            translation_domain=DOMAIN,
            translation_key="connection_error",
        ) from e
    except NtfyTimeoutError as e:
        raise ConfigEntryNotReady(
            translation_domain=DOMAIN,
            translation_key="timeout_error",
        ) from e

    coordinator = NtfyDataUpdateCoordinator(hass, entry, ntfy)
    await coordinator.async_config_entry_first_refresh()

    version = NtfyVersionDataUpdateCoordinator(hass, entry, ntfy)
    await version.async_config_entry_first_refresh()

    entry.runtime_data = NtfyRuntimeData(coordinator, version)

    dr.async_get(hass).async_get_or_create(
        config_entry_id=entry.entry_id, **coordinator.device_info
    )

    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

    entry.async_on_unload(entry.add_update_listener(_async_update_listener))

    return True


async def _async_update_listener(hass: HomeAssistant, entry: NtfyConfigEntry) -> None:
    """Handle update."""
    await hass.config_entries.async_reload(entry.entry_id)


async def async_unload_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool:
    """Unload a config entry."""
    return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
