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.SoftwareUpgrade import SoftwareUpgradeDto

logger = logging.getLogger(__name__)


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

    async def upgrade(self, *, dto: SoftwareUpgradeDto) -> None:
        nodes = self._nodes.listNodes(selector=dto.selector)

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

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