from clarabot_nano_common.common.request.gateway import NanoMutationIdCreateRequest
from clarabot_nano_common.common.request.server_general import (
    NanoFilesystemGetRequest,
    NanoFilesystemLsRequest,
    NanoFilesystemPeekRequest,
    NanoFilesystemThumbnailsRequest,
)

from clarabot_nano.common.request.gateway import (
    NanoCreateRoomConfigBlockRequest,
    AdminCreateSessionRequest,
    AdminLogStreamRequest,
    AdminLogReadRequest,
    NanoCreateAdhocTokenRequest,
    NanoResetAdhocTokensRequest,
    AddInterNanoJobRequest,
    DeleteSpecificInterNanoJobRequest,
    DescribeSpecificInterNanoJobRequest,
    ListInterNanoJobsRequest,
    NanoCountAdhocTokensRequest,
)
from clarabot_nano.common.request.server_document import (
    DocumentWsPrepareRequest,
    DocumentWsReadRequest,
    DocumentWsWriteRequest,
    DocumentWsOpenRequest,
    DocumentWsCloseRequest,
)
from clarabot_nano.common.request.server_fstx import (
    NanoFilesystemDeleteRequest,
    NanoFilesystemMkdirRequest,
    NanoFilesystemPutRequest,
    NanoFilesystemMoveRequest,
    AdminMkdirRequest,
    NanoFilesystemLockGetRequest,
    NanoFilesystemLockRequest,
)
from clarabot_nano.common.request.server_general import (
    NanoSearchRequest,
    AdminGetInfoRequest,
    AdminGetRemoteConfigRequest,
    AdminGetListingRequest,
)
from clarabot_nano.common.spawn.constants import ShortNames

# These do not require mutation-session tokens.
IDEMPOTENT_HANDLER_KEYS = set(
    request_cls.TYPE_KEY
    for request_cls in (
        NanoMutationIdCreateRequest,  # permanent lockout otherwise...
        AdminLogStreamRequest,
        AdminLogReadRequest,
        DescribeSpecificInterNanoJobRequest,
        ListInterNanoJobsRequest,
        NanoFilesystemGetRequest,
        NanoFilesystemLsRequest,
        NanoFilesystemPeekRequest,
        NanoFilesystemThumbnailsRequest,
        NanoSearchRequest,
        AdminGetInfoRequest,
        AdminGetRemoteConfigRequest,
        AdminGetListingRequest,
        NanoFilesystemLockGetRequest,
        DocumentWsPrepareRequest,
        NanoCountAdhocTokensRequest,
        # The document websocket write requests contain a strong random and timestamped unique session-id that is
        # identity-bound to the initiator, while the messages payload has a strict processing that makes replayed
        # requests invalid, making replay attacks infeasible ultimately. Not being restricted by the mutation-sessions
        # is a huge bonus because these requests need low latency and ad-hoc parallelism as well.
        DocumentWsWriteRequest,
        DocumentWsReadRequest,
    )
)

HANDLER_KEY_PREFIX_MAP = {}
for fstx_request_cls in (
    AdminMkdirRequest,
    NanoFilesystemDeleteRequest,
    NanoFilesystemMkdirRequest,
    NanoFilesystemMoveRequest,
    NanoFilesystemPutRequest,
    NanoFilesystemLockGetRequest,
    NanoFilesystemLockRequest,
):
    HANDLER_KEY_PREFIX_MAP[fstx_request_cls.TYPE_KEY] = ShortNames.FSTX_SERVER
for docu_request_cls in (
    DocumentWsPrepareRequest,
    DocumentWsOpenRequest,
    DocumentWsReadRequest,
    DocumentWsWriteRequest,
    DocumentWsCloseRequest,
):
    HANDLER_KEY_PREFIX_MAP[docu_request_cls.TYPE_KEY] = ShortNames.DOCUMENT_SERVER
for gate_request_cls in (
    AdminCreateSessionRequest,
    NanoMutationIdCreateRequest,
    NanoCreateRoomConfigBlockRequest,
    NanoCreateAdhocTokenRequest,
    NanoResetAdhocTokensRequest,
    NanoCountAdhocTokensRequest,
    AdminLogStreamRequest,
    AdminLogReadRequest,
    AddInterNanoJobRequest,
    DeleteSpecificInterNanoJobRequest,
    DescribeSpecificInterNanoJobRequest,
    ListInterNanoJobsRequest,
):
    HANDLER_KEY_PREFIX_MAP[gate_request_cls.TYPE_KEY] = ShortNames.GATEWAY


def get_server_prefix_from_request_type(req_type: str) -> str:
    return HANDLER_KEY_PREFIX_MAP.get(req_type, ShortNames.GENERAL_SERVER)
