from enum import Enum


def request_params(**params):
    def decorate(func):
        for item in params.items():
            key = item[0]
            value = item[1]
            func.__annotations__[key] = value
        return func
    return decorate


class DataType(str, Enum):
    
    """
    数据类型
    """

    BOOLEAN = "boolean"
    INTEGER = "integer"
    FLOAT = "float"
    STRING = "string"
    LIST = "list"
    DICT = "dict"
    FILE = "file"

    @classmethod
    def choices(cls):
        return tuple((x.value, x.name) for x in cls)

    def __str__(self):
        return self.value


class DataFrom(str, Enum):

    """
    数据来源：
    PATH     请求 URL 路径参数
    QUERY    请求 QUERY 参数
    HEADER   请求头参数
    COOKIE   请求 cookie 参数
    FORM     请求表单参数
    FILE     请求文件
    JSON     请求 body 参数
    RESPONSE 请求 RESPONSE 参数
    """

    PATH = "path"
    QUERY = "query"
    HEADER = "header"
    COOKIE = "cookie"
    FORM = "form"
    FILE = "file"
    JSON = "json"
    RESPONSE = "response"

    @classmethod
    def choices(cls):
        return tuple((x.value, x.name) for x in cls)

    def __str__(self):
        return self.value


class Param:

    """
    请求参数
    data_type: 数据类型
    data_from: 数据来源
    description: 描述信息
    """

    def __init__(self, data_type: DataType, data_from: DataFrom, desc: str = None):
        self.data_type = data_type.value if isinstance(data_type, DataType) else data_type
        self.data_from = data_from.value if isinstance(data_from, DataFrom) else data_from
        self.desc = desc if desc else ""

    def json(self):
        return {
            "type": self.data_type,
            "location": self.data_from,
            "desc": self.desc
        }


class ResponseParam(Param):

    """
    返回参数
    data_type: 数据类型
    description: 描述信息
    """

    def __init__(self, data_type: DataType, desc: str = None):
        self.data_type = data_type.value if isinstance(data_type, DataType) else data_type
        self.desc = desc if desc else ""

    def json(self):
        return {
            "type": self.data_type,
            "desc": self.desc
        }
    


class ResponseModel:
    """
    请求 Response 数据类型
    """

    def __init__(self, **params):
        self.params = params
        self._json = []
        for key, param in params.items():
            param_json = param.json()
            param_json["key"] = key
            self._json.append(param_json)

    def json(self):
        return self._json
