from rest_framework import response
from users.models import User
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers, status
from users.api.serializers import UserRegisterSerializer, UserSerializer, UserUpdateSerialier
from rest_framework.permissions import IsAuthenticated

class RegisterView(APIView):
    def post(self, request):
        serializer = UserRegisterSerializer(request.data)
        
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data)

        return Response(status = status.HTTP_400_BAD_REQUEST, data = serializer.errors)

class UserView(APIView):
    permission_classes =[IsAuthenticated]

    def get(self, request):
        serializer = UserSerializer(request.user)
        return Response(serializer.data)


    def put(self, request):
        user = User.objects.get(pk=request.user.id)
        serializer = UserUpdateSerialier(user, request.data) 

        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data)

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



        