from rest_framework import serializers
from django.contrib.auth.models import User
from .models import UserProfile, FieldInfo
    

# User Serializer
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        # fields = ('id', 'username', 'password' )
        fields = ('id', 'username', 'first_name', 'last_name', 'email')

# Register Serializer
class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'])

        return user


class UserProfileSerializer(serializers.ModelSerializer):
    username = serializers.CharField(source="user.username", read_only=True)
    email = serializers.CharField(source="user.email", read_only=True)
    first_name = serializers.CharField(source="user.first_name", read_only=True)
    last_name = serializers.CharField(source="user.last_name", read_only=True)
    designation_name = serializers.CharField(source="designation.designation_name", read_only=True)

    class Meta:
        model = UserProfile
        fields = '__all__'

class FieldInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = FieldInfo
        fields = '__all__'

class ChangePasswordSerializer(serializers.Serializer):
    model = User
    old_password = serializers.CharField(required=True)
    new_password = serializers.CharField(required=True)