from io import StringIO
from typing import Tuple, Optional

from clarabot_nano_common.common.error import log_escape_str
from clarabot_nano_common.common.handler.error import NanoHandlerError
from clarabot_nano_common.common.protocol.errors import (
    NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST,
    NANO_REQUEST_ERROR_HANDLER_DENIED,
    NANO_REQUEST_ERROR_INVALID_NEW_NAME,
    NANO_REQUEST_ERROR_HANDLER_TARGET_EXISTS,
    NANO_REQUEST_ERROR_LOCKED_PATH,
)
from clarabot_nano_common.common.protocol.request import NanoRequestContext
from clarabot_nano_common.common.protocol.response import NanoBaseResponse
from clarabot_nano_common.server.base.config import ServerConfig
from clarabot_nano_common.server.base.handler.base import ServerHandleHelper
from clarabot_nano_common.utility.file import path_stat_differs
from clarabot_nano_common.utility.path.chroot import NewNameValidationError
from clarabot_nano_common.utility.path.validate import ValidationError

from clarabot_nano.common.filesystem.operation.mkdir import MkdirOperation
from clarabot_nano.common.filesystem.tx_queue import TokenLockError
from clarabot_nano.common.request.server_fstx import NanoFilesystemMkdirRequest
from clarabot_nano.server.fstx.handler.base import AbstractFilesystemTransactorNanoHandler


class NanoFilesystemMkdirResponse(NanoBaseResponse):
    NORMALIZED_NAME = "norm_name"  # may be different from the input name due to normalization


class MakeDirectorySystemHandler(AbstractFilesystemTransactorNanoHandler):
    EXECUTION_STATE_CAN_CONFLICT = False  # cacheable and request_key are constant, no conflict is possible

    HANDLER_KEY = NanoFilesystemMkdirRequest.TYPE_KEY

    def __init__(self, helper: ServerHandleHelper, extracted_config: ServerConfig, context: dict, payload: dict):
        AbstractFilesystemTransactorNanoHandler.__init__(self, helper, extracted_config, context, payload)
        if not isinstance(self.payload.get(NanoFilesystemMkdirRequest.PATH), str):
            raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST, "Missing path", self)
        if not self.can_write_drive() and not self.can_upload_drive():  # anonymous is denied by mutation-id requirement
            raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_DENIED, "No permission", self)
        self.path = None

    async def _create_request_key(self, secure: bool = False) -> Tuple[bool, Optional[list], int]:
        return self._secure_request_key(False, None, 1) if secure else False, None, 1

    async def _execute_initial(self) -> dict:
        d = await AbstractFilesystemTransactorNanoHandler._execute_initial(self)
        try:
            self.path, s = self.chroot.new_path(
                self.payload[NanoFilesystemMkdirRequest.PATH],
                name_preprocess_fn=self.extracted_config.normalize_remote_path,
            )
            if s is not None:
                raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_TARGET_EXISTS, "Already exists", self)
            async with self.helper.fstx_queue.wait_ready(
                MkdirOperation(self.context[NanoRequestContext.ACCOUNT_ID], self.path.as_posix())
            ) as submitter:
                # Final check before execution. This can fail if contesting operations have changed the filesystem.
                path2, s2 = self.chroot.new_path(
                    self.payload[NanoFilesystemMkdirRequest.PATH],
                    name_preprocess_fn=self.extracted_config.normalize_remote_path,
                )
                if s2 is not None:  # Prioritize having more accurate error than the generic contest-fail.
                    raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_TARGET_EXISTS, "Already exists", self)
                if path_stat_differs(self.path, s, path2, s2):
                    raise ValidationError("Depending paths are contested")
                await submitter()  # TODO: timeout
        except NewNameValidationError as e:
            raise NanoHandlerError(NANO_REQUEST_ERROR_INVALID_NEW_NAME, "Invalid name", self) from e
        except ValidationError as e:
            raise NanoHandlerError(
                NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST, "Invalid path", self, maybe_conflict=True
            ) from e
        except OSError as e:
            raise NanoHandlerError(
                NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST, "Unresolved path", self, maybe_conflict=True
            ) from e
        except TokenLockError as e:
            raise NanoHandlerError(NANO_REQUEST_ERROR_LOCKED_PATH, "Locked path", self) from e
        d[NanoFilesystemMkdirResponse.NORMALIZED_NAME] = self.path.name
        return d

    async def _execute_part(self, part: int, s_hmac_start_part: Optional[int]) -> dict:  # pragma: no cover
        raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST, "Part request is invalid", self)

    def _log_message(self) -> str:
        return f"Make directory {log_escape_str(self.path)}"

    def build_error_message(self, buf: StringIO) -> None:
        buf.write("Make directory ")
        buf.write(log_escape_str(self.payload.get(NanoFilesystemMkdirRequest.PATH)))
        buf.write(" in ")
        buf.write(log_escape_str(self.get_drive_root_for_error_message()))
