"""Provider command handler registry and routing."""

from __future__ import annotations

import logging

from .base import ProviderCommandHandler

_LOGGER = logging.getLogger(__name__)

_REGISTRY: dict[str, ProviderCommandHandler] = {}


def register_handler(handler: ProviderCommandHandler) -> None:
    """Register a provider command handler."""
    _REGISTRY[handler.provider_type] = handler
    _LOGGER.debug("Registered command handler for provider type: %s", handler.provider_type)


def get_handler(provider_type: str) -> ProviderCommandHandler | None:
    """Get the command handler for a provider type."""
    return _REGISTRY.get(provider_type)


def get_all_handlers() -> dict[str, ProviderCommandHandler]:
    """Get all registered handlers."""
    return dict(_REGISTRY)


def _auto_register() -> None:
    """Auto-register all built-in handlers."""
    from .immich import ImmichCommandHandler
    from .gitea_handler import GiteaCommandHandler
    from .planka_handler import PlankaCommandHandler
    from .nut_handler import NutCommandHandler
    from .webhook_handler import WebhookCommandHandler
    from .home_assistant_handler import HomeAssistantCommandHandler
    from .bridge_self_handler import BridgeSelfCommandHandler

    register_handler(ImmichCommandHandler())
    register_handler(GiteaCommandHandler())
    register_handler(PlankaCommandHandler())
    register_handler(NutCommandHandler())
    register_handler(WebhookCommandHandler())
    register_handler(HomeAssistantCommandHandler())
    register_handler(BridgeSelfCommandHandler())


# Auto-register on import
_auto_register()
