import logging

from gateway_core.domain.Reboot import RebootDto
from gateway_node_agent.application.ports.rebootExecutorPort import RebootExecutorPort

logger = logging.getLogger(__name__)


class LocalRebootUseCase:
    """
    Selects a matching reboot adapter and triggers a reboot.

    The use case iterates over the configured reboot adapters, selects the first
    one that can handle the given reboot request, and delegates the execution to
    it.

    :param rebootAdapters: List of reboot adapters available for this use case.
    """

    def __init__(self, *, rebootAdapters: list[RebootExecutorPort]) -> None:
        self._rebootAdapters = rebootAdapters

    async def execute(self, dto: RebootDto) -> None:
        for adapter in self._rebootAdapters:
            if adapter.canHandle(dto):
                logger.info(
                    "LocalRebootUseCase using %s for %s",
                    adapter.__class__.__name__,
                    dto,
                )
                await adapter.execute(dto)
                return

        raise RuntimeError(
            f"Unsupported reboot scenario: type={dto.type} entity={dto.entity}"
        )
