from rest_framework import serializers
from django.contrib.auth.models import User,Group

from .models import Flight, Booking, Profile


class FlightSerializer(serializers.ModelSerializer):
	class Meta:
		model = Flight
		fields = ['destination', 'time', 'price', 'id']


class BookingSerializer(serializers.ModelSerializer):
	class Meta:
		model = Booking
		fields = ['flight', 'date', 'id']


class BookingDetailsSerializer(serializers.ModelSerializer):
	class Meta:
		model = Booking
		fields = ['flight', 'date', 'passengers', 'id']


class AdminUpdateBookingSerializer(serializers.ModelSerializer):
	class Meta:
		model = Booking
		fields = ['date', 'passengers']


class UpdateBookingSerializer(serializers.ModelSerializer):
	class Meta:
		model = Booking
		fields = ['passengers']


class RegisterSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True)
    class Meta:
        model = User
        fields = ['username', 'password', 'first_name', 'last_name']

    def create(self, validated_data):
        username = validated_data['username']
        password = validated_data['password']
        first_name = validated_data['first_name']
        last_name = validated_data['last_name']
        new_user = User(username=username, first_name=first_name, last_name=last_name)
        new_user.set_password(password)
        new_user.save()
        return validated_data


class GroupSerializer(serializers.ModelSerializer):
	class Meta:
		model = Group
		fields = '__all__'

	def create(self, validated_data):

		print("VALIDATED DATA: ",validated_data)
		data = validated_data
		# permission_data = validated_data['permissions']
		permission_data = validated_data.pop('permissions')
		new_group = Group.objects.create(name=validated_data['name'])
		new_group.permissions.add(*permission_data)
		new_group.save()
		# new_group = Group.objects.get(name = validated_data['name'])
		# new_group.permissions.add(*permission_data)
		# # for permission in permission_data:
		# # 	print(permission)
		# # 	new_group.permissions.add(permission.id)
		# new_group.save()
		return data


class ProfileSerializer(serializers.ModelSerializer):
	class Meta:
		model = Profile
		fields = ['user', 'miles']

