# retoor <retoor@molodetz.nl>

from __future__ import annotations

from devplacepy.services.base import BaseService, ConfigField

from .hub import pubsub


class PubSubService(BaseService):
    title = "Pub/Sub"
    description = (
        "In-process publish/subscribe bus over websockets. Lets the frontend, services and "
        "Devii broadcast and receive messages on named topics. Database-free and ephemeral; "
        "served on the service lock owner so every subscriber converges on one worker."
    )
    default_enabled = True

    def __init__(self):
        super().__init__(name="pubsub", interval_seconds=3600)
        self.config_fields = [
            ConfigField(
                "pubsub_allow_guests",
                "Allow guests",
                type="bool",
                default=False,
                help="Allow unauthenticated guests to subscribe to public.* topics.",
                group="Pub/Sub",
            ),
        ]

    async def run_once(self) -> None:
        return None

    def collect_metrics(self) -> dict:
        return {"topics": len(pubsub.topics())}
