from django.contrib.auth import get_user_model
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from django.contrib.auth.hashers import make_password
from rest_framework import status
from rest_framework_simplejwt.views import TokenObtainPairView

from core.serializers import (
        UserSerializer, MyTokenObtainPairSerializer, 
        UserSerializerWithToken,
)

User = get_user_model()

# OBTAINING A TOKEN WHEN USER IS LOGGED IN
class MyTokenObtainPairView(TokenObtainPairView):
    serializer_class = MyTokenObtainPairSerializer

@api_view(['POST'])
def registerUser(request):
    data = request.data

    try:
        user = User.objects.create(
            first_name = data['name'],
            username = data['email'],
            email = data['email'],
            password = make_password(data['password'])
        )

        serializer = UserSerializerWithToken(user, many=False)
        return Response(serializer.data)
    except:
        message = {'detail': 'User with this mail already exist'}
        return Response(message, status=status.HTTP_400_BAD_REQUEST)

# SINGLE USER PROFILE WITH AUTHENTICATION
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getUserProfile(request):
    user = request.user
    serializers = UserSerializer(user, many=False)
    return Response(serializers.data)

@api_view(['PUT'])
@permission_classes([IsAuthenticated])
def updateUserProfile(request):
    user = request.user
    serializers = UserSerializerWithToken(user, many=False)

    data = request.data
    print(data)

    user.first_name = data['name']
    user.email = data['email']
    user.username = data['email']

    if data['password'] != '':
        user.password = make_password(data['password'])
    
    user.save()

    return Response(serializers.data)

# ADMIN WOULD BE ABLE TO SEE ALL USERS
@api_view(['GET'])
@permission_classes([IsAdminUser])
def getUsers(request):
    users = User.objects.all()
    serializers = UserSerializer(users, many=True)
    return Response(serializers.data)

# ADMIN WOULD BE ABLE TO DELETE A SINGLE USER
@api_view(['DELETE'])
@permission_classes([IsAdminUser])
def deleteUser(request,pk):
    user = User.objects.get(pk=pk)
    user.delete()
    return Response('User Deleted')

@api_view(['GET'])
@permission_classes([IsAdminUser])
def getUsersById(request,pk):
    user = User.objects.get(pk=pk)
    serializers = UserSerializer(user, many=False)
    return Response(serializers.data)

# ADMIN WOULD BE ABLE TO MODIFY A USERS DATA
@api_view(['PUT'])
@permission_classes([IsAdminUser])
def updateUser(request,pk):
    user = User.objects.get(pk=pk)

    data = request.data

    user.first_name = data['name']
    user.email = data['email']
    user.username = data['email']
    user.is_staff = data['isAdmin']
    
    user.save()

    serializers = UserSerializer(user, many=False)
    return Response(serializers.data)