from collections.abc import Callable, Sequence
from typing import Any

from csirtmu.core.temporal import TemporalActivities
from csirtmu.core.temporal.s3 import S3Config
from temporalio import activity

from csirtmu.perun_sync.config import Config
from csirtmu.perun_sync.perun_sync.types import PerunDatabaseSyncResult
from csirtmu.perun_sync.temporal import activities_impl


class PerunSyncActivities(TemporalActivities):
    def __init__(self, config: S3Config, listen_timeout: int) -> None:
        self.config = config
        self.s3_listen_timeout = listen_timeout

    @activity.defn
    async def s3_get_latest_object(self) -> str | None:
        return activities_impl.s3_get_latest_object(self.config)

    @activity.defn
    async def s3_listen_for_new_object(self) -> str | None:
        return await activities_impl.s3_listen_for_new_object(self.config, self.s3_listen_timeout)

    def get_activities(self) -> Sequence[Callable[..., Any]]:
        return [
            self.s3_get_latest_object,
            self.s3_listen_for_new_object,
        ]


class PerunAlphaSyncActivities(TemporalActivities):
    def __init__(self, config: Config) -> None:
        self.config = config

    @activity.defn
    async def sync_to_database(self, s3_key: str, dry_run: bool) -> PerunDatabaseSyncResult:
        return await activities_impl.sync_to_database(self.config.s3, self.config.database.engine_url, s3_key, dry_run)

    def get_activities(self) -> Sequence[Callable[..., Any]]:
        return [
            self.sync_to_database,
        ]
