from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.reverse import reverse
from rest_framework_simplejwt.tokens import RefreshToken


@api_view(['GET'])
def root(request, fmt=None):
    return Response({
        'v1': reverse('api_v1:root', request=request, format=fmt),
    })


@api_view(['GET'])
def v1_root(request, fmt=None):
    root_navigation = {
        'redirects': reverse('api_v1:redirects:redirect-list', request=request, format=fmt),
        'token': reverse('api_v1:token_root', request=request, format=fmt)
    }
    return Response(root_navigation)


@api_view(['GET'])
def token_root(request, fmt=None):
    token_navigation = {
        'auth': reverse('api_v1:token_auth', request=request, format=fmt),
        'refresh': reverse('api_v1:token_refresh', request=request, format=fmt),
        'verify': reverse('api_v1:token_verify', request=request, format=fmt),
    }
    return Response(token_navigation)


@api_view(['POST'])
def token_refresh(request):
    token = request.COOKIES.get("burl_refresh_token")
    if token:
        refresh = RefreshToken(str(token))
        access = str(refresh.access_token)
        if access:
            return Response({"access": access}, 200)
        else:
            return Response({"unauthorized"}, 401)
    return Response("unauthorized", 401)


@api_view(['POST'])
def token_refresh_revoke(_request):
    response = Response("ok")
    response.delete_cookie("burl_refresh_token")
    return response

[(7, 'rest_framework.decorators.api_view', 'api_view', False, 'from rest_framework.decorators import api_view\n'), (14, 'rest_framework.decorators.api_view', 'api_view', False, 'from rest_framework.decorators import api_view\n'), (23, 'rest_framework.decorators.api_view', 'api_view', False, 'from rest_framework.decorators import api_view\n'), (33, 'rest_framework.decorators.api_view', 'api_view', False, 'from rest_framework.decorators import api_view\n'), (46, 'rest_framework.decorators.api_view', 'api_view', False, 'from rest_framework.decorators import api_view\n'), (20, 'rest_framework.response.Response', 'Response', False, 'from rest_framework.response import Response\n'), (30, 'rest_framework.response.Response', 'Response', False, 'from rest_framework.response import Response\n'), (43, 'rest_framework.response.Response', 'Response', False, 'from rest_framework.response import Response\n'), (48, 'rest_framework.response.Response', 'Response', False, 'from rest_framework.response import Response\n'), (17, 'rest_framework.reverse.reverse', 'reverse', False, 'from rest_framework.reverse import reverse\n'), (18, 'rest_framework.reverse.reverse', 'reverse', False, 'from rest_framework.reverse import reverse\n'), (26, 'rest_framework.reverse.reverse', 'reverse', False, 'from rest_framework.reverse import reverse\n'), (27, 'rest_framework.reverse.reverse', 'reverse', False, 'from rest_framework.reverse import reverse\n'), (28, 'rest_framework.reverse.reverse', 'reverse', False, 'from rest_framework.reverse import reverse\n'), (10, 'rest_framework.reverse.reverse', 'reverse', False, 'from rest_framework.reverse import reverse\n'), (40, 'rest_framework.response.Response', 'Response', False, 'from rest_framework.response import Response\n'), (42, 'rest_framework.response.Response', 'Response', False, 'from rest_framework.response import Response\n')]