from typing import Tuple, Optional

from clarabot_nano.common.handler.base import NanoHandlerError
from clarabot_nano.common.protocol.errors import NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST
from clarabot_nano.common.protocol.response import NanoBaseResponse
from clarabot_nano.common.request.server_general import AdminGetRemoteConfigRequest
from clarabot_nano.server.base.handler.base import AbstractNanoServerHandler


class AdminGetRemoteConfigResponse(NanoBaseResponse):
    CONFIG = "config"


class GetRemoteConfigHandler(AbstractNanoServerHandler):
    REQUIRES_OWNER_IDENTITY = True
    REQUIRES_ADMIN_PASSWORD = True

    HANDLER_KEY = AdminGetRemoteConfigRequest.TYPE_KEY

    async def _create_request_key(self) -> Tuple[bool, Optional[list], int]:
        return False, None, 1

    async def _execute_initial(self) -> dict:
        d = await AbstractNanoServerHandler._execute_initial(self)
        d[AdminGetRemoteConfigResponse.CONFIG] = self.extracted_config.remote_config
        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.context)

    def _log_message(self) -> str:
        return "Nano remote config queried"
