from clarabot_nano_common.common.protocol.request import NanoBaseRequest


class AdminMkdirRequest(NanoBaseRequest):
    TYPE_KEY = "A-MKD"

    PATH = "path"
    NAME = "name"


class NanoFilesystemMoveRequest(NanoBaseRequest):
    TYPE_KEY = "FS-MOV"

    SRC_PATH = "src_path"  # source
    NEW_PATH = "new_path"  # target (must not exist)


class NanoFilesystemDeleteRequest(NanoBaseRequest):
    TYPE_KEY = "FS-DEL"

    PATH = "path"


class NanoFilesystemMkdirRequest(NanoBaseRequest):
    TYPE_KEY = "FS-MKD"

    PATH = "path"


class NanoFilesystemLockGetRequest(NanoBaseRequest):
    TYPE_KEY = "FS-L-G"

    PATH = "path"


class NanoFilesystemLockRequest(NanoBaseRequest):
    TYPE_KEY = "FS-L-X"

    PATH = "path"
    OP = "op"
    TOKEN = "token"
    OLD = "old"  # only for replace

    # internal only options
    EDD = "edd"  # ensure drive directory (before locking)
    NOR = "nor"  # no-resolve, allows locking non-existing paths (in advance of operations)


class NanoFilesystemPutRequest(NanoBaseRequest):
    """
    Some of the fields have special meanings that trigger upload reset or finalization:
        - if OFFSET is 0 then the request starts or restarts an upload
        - if SIZE or MTIME for any part will mismatch the values from the (re)starting request, conflict error
          will be returned and the upload must be restarted
        - SMAC must be specified on the last part and it must be the hash of the hashes of all parts in order
        - the OFFSETs of all parts must build a range from 0 without gaps or overlaps
    """

    TYPE_KEY = "FS-PUT"

    # consistent path to upload the file to
    PATH = "path"  # final target path of the upload
    # metadata fields - may be omitted if not available, but must be consistent for all parts
    SIZE = "size"  # final size of the file in bytes
    MTIME = "mtime"  # unix timestamp when the file was last modified; in milliseconds
    MTFRAC = "mtfrac"  # fractional part of UNIX timestamp prefixed with "1" (usually 6 or 9 digits depending on fs)
    # data chunking
    OFFSET = "offset"  # number of bytes to offset the writing of the data chunk
    DATA = "data"  # data chunk, optimally ~1MB
    # upload finalization
    SMAC = "smac"  # sum-hash of all parts in order
    # behaviour changes
    OVERWRITE = "overwrite"  # to allow overwriting the target path
    AUTO_NAME = "auto_name"  # automatically select an upload location and filename based on this name
