from concurrent.futures.thread import ThreadPoolExecutor
from pathlib import Path
from typing import Union

from clarabot_nano_common.server.base.handler.base import ServerHandleHelper
from clarabot_nano_common.server.base.handler.filesystem.base import AbstractFileSystemHandler
from clarabot_nano_common.utility.path.security import set_remote_file_security

from clarabot_nano.common.filesystem.tx_queue import FilesystemTxQueue
from clarabot_nano.server.fstx.tmpfiledb import FstxTemporaryFileDatabase


class FilesystemTransactorServerHandleHelper(ServerHandleHelper):
    def __init__(
        self, pool: ThreadPoolExecutor, temp_file_db: FstxTemporaryFileDatabase, fstx_queue: FilesystemTxQueue
    ):
        ServerHandleHelper.__init__(self, pool)
        self.temp_file_db = temp_file_db
        self.fstx_queue = fstx_queue


class AbstractFilesystemTransactorNanoHandler(AbstractFileSystemHandler):
    # fix type hinting
    helper: FilesystemTransactorServerHandleHelper

    def _set_remote_file_security(self, path: Union[str, Path]):
        if self.extracted_config.remote_files_restriction:
            set_remote_file_security(path)
