from typing import List, Optional

from messagebus import SubscriptionInfo
from messageprocessor import DefaultMessageProcessorExecutorFactory, MessageProcessorCallback

from rediseventlock._TenantRedisClient import TenantRedisClient
from rediseventlock.callback import EventLockCallback
from ._EventLockMessageProcessorExecutor import EventLockMessageProcessorExecutor


class EventLockMessageProcessorExecutorFactory(DefaultMessageProcessorExecutorFactory):
    def __init__(
        self,
        callbacks: List[MessageProcessorCallback],
        tenant_redis: TenantRedisClient,
        subscriptions: Optional[List[SubscriptionInfo]] = None,
        lock_timeout: int = 1200,
    ):
        super().__init__(callbacks)
        self.tenant_redis = tenant_redis
        self.subscriptions = subscriptions or []
        self.lock_timeout = lock_timeout

    def build_executor(self, message_processor):
        return EventLockMessageProcessorExecutor(
            event_processor=message_processor,
            callbacks=[
                *(self.callbacks or []),
                EventLockCallback(
                    redis_client=self.tenant_redis,
                    subscriptions=self.subscriptions,
                    lock_timeout=self.lock_timeout,
                ),
            ],
        )
