"""Services for the Netatmo integration."""

from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import issue_registry as ir

from .const import DOMAIN
from .coordinator import NetatmoConfigEntry, async_get_loaded_entry
from .webhook import async_register_webhook, async_unregister_webhook

SERVICE_REGISTER_WEBHOOK = "register_webhook"
SERVICE_UNREGISTER_WEBHOOK = "unregister_webhook"


def _get_loaded_entry(hass: HomeAssistant) -> NetatmoConfigEntry:
    """Return the loaded config entry or raise if unavailable."""
    if (entry := async_get_loaded_entry(hass)) is None:
        raise ServiceValidationError(
            translation_domain=DOMAIN,
            translation_key="entry_not_loaded",
        )
    return entry


def _deprecate(hass: HomeAssistant, service: str) -> None:
    """Warn and raise a repair issue for the deprecated webhook actions."""
    ir.async_create_issue(
        hass,
        DOMAIN,
        f"deprecated_service_{service}",
        breaks_in_ha_version="2027.2.0",
        is_fixable=False,
        severity=ir.IssueSeverity.WARNING,
        translation_key="deprecated_webhook_service",
    )


async def _register_webhook(call: ServiceCall) -> None:
    """Handle the deprecated register_webhook action."""
    _deprecate(call.hass, SERVICE_REGISTER_WEBHOOK)
    entry = _get_loaded_entry(call.hass)
    # Drop any existing registration first so re-registering an already-active
    # webhook does not raise "Handler is already defined!".
    await async_unregister_webhook(call.hass, entry)
    await async_register_webhook(call.hass, entry)


async def _unregister_webhook(call: ServiceCall) -> None:
    """Handle the deprecated unregister_webhook action."""
    _deprecate(call.hass, SERVICE_UNREGISTER_WEBHOOK)
    await async_unregister_webhook(call.hass, _get_loaded_entry(call.hass))


@callback
def async_setup_services(hass: HomeAssistant) -> None:
    """Register the Netatmo services."""
    hass.services.async_register(DOMAIN, SERVICE_REGISTER_WEBHOOK, _register_webhook)
    hass.services.async_register(
        DOMAIN, SERVICE_UNREGISTER_WEBHOOK, _unregister_webhook
    )
