from typing import Protocol, List, Optional

from bussdcc.context import ContextProtocol
from bussdcc.event import Event
from bussdcc.message import Message

from .info import ServiceInfo


class ServiceProtocol(Protocol):
    name: str
    interval: float  # seconds between ticks
    enabled: bool  # start at boot
    restart: bool  # restart if it crashes
    critical: bool  # if True, system may halt on failure

    def attach(self, ctx: ContextProtocol) -> None: ...
    def detach(self) -> None: ...

    def start(self, ctx: ContextProtocol) -> None: ...
    def tick(self, ctx: ContextProtocol) -> None: ...
    def stop(self, ctx: ContextProtocol) -> None: ...

    def handle_event(self, ctx: ContextProtocol, evt: Event[Message]) -> None: ...


class ServiceSupervisorProtocol(Protocol):
    def boot(self) -> None: ...
    def shutdown(self) -> None: ...
    def register(self, service: ServiceProtocol) -> None: ...
    def names(self) -> list[str]: ...
    def list(self) -> list[ServiceProtocol]: ...
    def get(self, name: str) -> Optional[ServiceProtocol]: ...
    def status(self, name: str) -> ServiceInfo: ...
    def statuses(self) -> List[ServiceInfo]: ...
    def stop_all(self) -> None: ...
    def start(self, name: str) -> None: ...
    def is_running(self, name: str) -> bool: ...
    def running(self) -> List[ServiceProtocol]: ...
    def stop(self, name: str) -> None: ...
    def enable(self, name: str) -> None: ...
    def disable(self, name: str) -> None: ...
