from __future__ import annotations

import logging

from gateway_core.application.ports.NodeAgentPort import NodeAgentPort
from gateway_core.application.ports.NodesPort import NodesPort
from gateway_core.domain.Reboot import RebootDto

logger = logging.getLogger(__name__)


class RebootOrchestrator:
    def __init__(self, *, nodes: NodesPort, nodeAgent: NodeAgentPort) -> None:
        self._nodes = nodes
        self._nodeAgent = nodeAgent

    async def reboot(self, *, dto: RebootDto) -> None:
        if dto.selector is None:
            raise ValueError("RebootDto.selector must be set for orchestration")

        nodes = self._nodes.listNodes(selector=dto.selector)

        logger.info(
            "RebootOrchestrator: rebooting %d node(s): %s",
            len(nodes),
            [n.nodeId for n in nodes],
        )

        for node in nodes:
            await self._nodeAgent.triggerReboot(nodeInfo=node, dto=dto)
