from functools import wraps

from jsons import load
from requests import Response, get

from wapi.static.T import T
from wapi.utils.get_path import get_path


def GET(
        path: str = "",
        _T: T = None,
        *,
        auto: bool = False,
        unpack: bool = False
) -> T | Response:
    def decorator(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):
            headers = kwargs.pop("headers", None)
            p = get_path(self, func, path)
            url = p.format(*args, **kwargs)
            if auto:
                if unpack and (body := kwargs.pop('body', None)):
                    kwargs.update(body)

                url += "?" + "&".join([f'{k}={v}' for k, v in kwargs.items()])

            response = get(url, headers=headers)

            if _T is None:
                return response
            return load(response.json(), _T)

        return wrapper

    return decorator
