from .models import RegisteredUser
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from .serializers import RegisteredUserSerializer, CreateRegisteredUserThroughUserSerializer, CreateRegisteredUserSerializer
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from django.contrib.auth import authenticate
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes
from rest_framework.decorators import permission_classes
import json


@api_view(['GET'])
def all_registered_users(request):
    users = RegisteredUser.objects.all()
    serializer = RegisteredUserSerializer(users, many=True)
    return Response(data=serializer.data, status=status.HTTP_200_OK)


@api_view(['POST'])
def create_registered_user(request):
    _serializer = CreateRegisteredUserSerializer(data=request.data)
    
    if not _serializer.is_valid():
        return Response(_serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    email = request.data["email"]

    if User.objects.filter(email=email):
        return Response({"email": [ "Email in use" ] }, status=status.HTTP_409_CONFLICT)

    serializer = CreateRegisteredUserThroughUserSerializer(data=request.data)
    if serializer.is_valid():
        new_registered_user = serializer.save()
        serializer = RegisteredUserSerializer(new_registered_user, many=False)

        token = Token.objects.get(user=new_registered_user.user).key
        data = serializer.data
        data["token"] = token
        return Response(data,status=status.HTTP_201_CREATED)

    return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    

@api_view(['POST'])
def login_registered_user(request):
    email = request.data["email"]
    password = request.data["password"]
    
    user = authenticate(username=email, password=password)

    if user is not None:
        registered_user = RegisteredUser.objects.get(user=user)
        serializer = RegisteredUserSerializer(registered_user, many=False)
        token = Token.objects.get(user=registered_user.user).key
        data = serializer.data
        data["token"] = token
        return Response(data,status=status.HTTP_200_OK)
    else:
        return Response({}, status=status.HTTP_401_UNAUTHORIZED)


@api_view(['POST'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def update_registered_user(request):
    registered_user = RegisteredUser.objects.get(user=request.user)

    email = request.POST.get("email")
    first_name = request.POST.get("first_name")
    last_name = request.POST.get("last_name")
    image = request.POST.get("image")
    
    registered_user.first_name = first_name
    registered_user.last_name = last_name
    registered_user.user.email = email
    registered_user.image = image
    
    registered_user.save()
    registered_user.user.save()

    registered_user = RegisteredUser.objects.get(user=request.user)
    serializer = RegisteredUserSerializer(registered_user, many=False)
    token = Token.objects.get(user=registered_user.user).key
    data = serializer.data
    data["token"] = token
    return Response(data,status=status.HTTP_200_OK)


@api_view(['POST'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def update_password(request):
    current_password = request.POST.get("current_password")
    new_password = request.POST.get("new_password")
    user = request.user
    
    success = user.check_password(current_password)
    if success: 
        user.set_password(new_password)
        user.save()
        registered_user = RegisteredUser.objects.get(user=request.user)
        serializer = RegisteredUserSerializer(registered_user, many=False)
        token = Token.objects.get(user=registered_user.user).key
        data = serializer.data
        data["token"] = token
        return Response(data,status=status.HTTP_200_OK)
    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)