from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.permissions import AllowAny, IsAuthenticated
from . import serializers
from apps.users.models import Profile
from apps.users.utils import get_and_authenticate_user, create_user_account, create_profile_user
import json
User = get_user_model()

class AuthViewSet(viewsets.GenericViewSet):
    permission_classes = [AllowAny, ]
    serializer_class = serializers.EmptySerializer
    serializer_classes = {
        'login': serializers.UserLoginSerializer,
        'register': serializers.UserRegisterSerializer,
        'password_change': serializers.PasswordChangeSerializer,
        'profile': serializers.ProfileSerializer
    }

    @action(methods=['POST', ], detail=False)
    def login(self, request):
        # request = json.loads(request.POST.get('data'))
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = get_and_authenticate_user(**serializer.validated_data)
        data = serializers.AuthUserSerializer(user).data
        # print(data)
        try:
            data.update({'users': Profile.objects.get(user_id=user.id).id})
        except:
            pass
        return Response(data=data, status=status.HTTP_200_OK)

    @action(methods=['POST', ], detail=False)
    def register(self, request):
        print(request.data['data'])
        # request = json.loads(request.POST.get('data'))
        serializer = self.get_serializer(data=request.data['data'])
        serializer.is_valid(raise_exception=True)
        # del serializer.validated_data['Profile']
        profile = serializer.validated_data.pop('profile')
        user = create_user_account(**serializer.validated_data)
        print('user', user.id)
        profile.update({'user': user.id})
        profile_serializer = serializers.ProfileSerializer(data=profile)
        profile_serializer.is_valid(raise_exception=True)
        profile_data = create_profile_user(**profile_serializer.validated_data)
        data = serializers.AuthUserSerializer(user).data
        return Response(data=data, status=status.HTTP_201_CREATED)

    @action(methods=['POST', ], detail=False)
    def logout(self, request):
        logout(request)
        data = {'success': 'Sucessfully logged out'}
        return Response(data=data, status=status.HTTP_200_OK)


    @action(methods=['POST'], detail=False, permission_classes=[IsAuthenticated, ])
    def password_change(self, request):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        request.user.set_password(serializer.validated_data['new_password'])
        request.user.save()
        return Response(status=status.HTTP_204_NO_CONTENT)

    def get_serializer_class(self):
        if not isinstance(self.serializer_classes, dict):
            raise ImproperlyConfigured("serializer_classes should be a dict mapping.")

        if self.action in self.serializer_classes.keys():
            return self.serializer_classes[self.action]
        return super().get_serializer_class()
