from dataclasses import dataclass

from gateway_core.domain.Reboot import RebootDto, RebootEntity, RebootType
from gateway_node_agent.application.ports.rebootExecutorPort import RebootExecutorPort
from gateway_platform.logind.LogindManagerClient import LogindManagerClient


@dataclass(frozen=True, slots=True)
class LogindRebootAdapter(RebootExecutorPort):
    """
    Infrastructure adapter for executing OS-level reboots via systemd-logind
    using DBus.

    Implemented as an immutable, slot-based dataclass to keep the adapter
    stateless, memory-efficient, and safe against accidental runtime mutation.
    The adapter is instantiated once during application composition and reused
    for the entire application lifetime.

    :param client: systemd-logind DBus client used to trigger the reboot.
    :param isArmed: Safety flag to enable or disable reboot execution.
    """

    client: LogindManagerClient
    isArmed: bool = False

    def canHandle(self, dto: RebootDto) -> bool:
        return dto.entity == RebootEntity.os

    async def execute(self, dto: RebootDto) -> None:
        if not self.isArmed:
            return
        await self.client.reboot()

