# retoor <retoor@molodetz.nl>

from __future__ import annotations

from devplacepy.services.manager import ServiceManager

_SHELL_MANAGER: ServiceManager | None = None


def _instantiate_all():
    from devplacepy.services.audit import AuditService
    from devplacepy.services.backup.service import BackupService
    from devplacepy.services.bot.service import BotsService
    from devplacepy.services.containers.service import ContainerService
    from devplacepy.services.devii import DeviiService
    from devplacepy.services.gitea.service import IssueTrackerService
    from devplacepy.services.news import NewsService
    from devplacepy.services.openai_gateway import GatewayService
    from devplacepy.services.pubsub import PubSubService
    from devplacepy.services.telegram.outbox_service import TelegramOutboxService
    from devplacepy.services.telegram.service import TelegramService
    from devplacepy.services.xmlrpc import XmlrpcService

    return (
        AuditService(),
        BackupService(),
        BotsService(),
        ContainerService(),
        DeviiService(),
        IssueTrackerService(),
        NewsService(),
        GatewayService(),
        PubSubService(),
        TelegramService(),
        TelegramOutboxService(),
        XmlrpcService(),
    )


def admin_shell_manager() -> ServiceManager:
    # Never supervised (no set_lock_owner/supervise call): only used for describe()/config metadata/key derivation, since the owning Tier 3 process is the only one that ever ticks these services.
    global _SHELL_MANAGER
    if _SHELL_MANAGER is None:
        manager = ServiceManager()
        for svc in _instantiate_all():
            manager.register(svc)
        _SHELL_MANAGER = manager
    return _SHELL_MANAGER
