import logging
from typing import Tuple, Optional

from clarabot_nano import __version__
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 AdminGetInfoRequest
from clarabot_nano.server.base.handler.base import AbstractNanoServerHandler


class AdminGetInfoResponse(NanoBaseResponse):
    NAME = "name"
    VERSION = "version"


class GetInfoHandler(AbstractNanoServerHandler):
    REQUIRES_OWNER_IDENTITY = True  # NOTE: No password to allow listing all Nanos with name easily.

    HANDLER_KEY = AdminGetInfoRequest.TYPE_KEY
    SUCCESS_LOG_LEVEL = logging.DEBUG  # do not log this normally

    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[AdminGetInfoResponse.NAME] = self.extracted_config.name
        d[AdminGetInfoResponse.VERSION] = __version__
        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 info queried"
