import abc
import asyncio
from collections.abc import Sequence
from typing import Any

from csirtmu.core.temporal import TemporalWorkflow, create_temporal_worker

from csirtmu.perun_sync.alias_sync.workflow import AliasSyncWorkflow
from csirtmu.perun_sync.config import AppConfig
from csirtmu.perun_sync.temporal.unique_workflow_worker import start_unique_workflow
from csirtmu.perun_sync.temporal.workflow import PerunAlphaSyncWorkflow

WORKFLOWS: Sequence[type[TemporalWorkflow]] = [
    PerunAlphaSyncWorkflow,
    AliasSyncWorkflow,
]


class TemporalWorker(abc.ABC):
    name: str

    @abc.abstractmethod
    async def start(self) -> None:
        raise NotImplementedError

    def __init_subclass__(cls, **kwargs: Any) -> None:
        super().__init_subclass__(**kwargs)
        if not hasattr(cls, "name"):
            cls.name = cls.__name__


class PerunSyncWorker(TemporalWorker):
    name = "perun_sync"

    async def start(self) -> None:
        config = AppConfig.get()
        worker = await create_temporal_worker(WORKFLOWS, task_queue=config.temporal.default_task_queue, config=config)
        await start_unique_workflow(PerunAlphaSyncWorkflow, config.workflow_id, config.temporal)
        await worker.run()


if __name__ == "__main__":
    asyncio.run(PerunSyncWorker().start())
