from django import forms
from rest_framework import serializers, generics
from django.contrib.auth import get_user_model, password_validation
from django.contrib.auth.models import User, BaseUserManager
from django.contrib.auth.forms import UserCreationForm


from .models import (
    Profile,
    Contact,
    OtherContact,
    EducationalBackground,
    Skill,
    WorkExperience,
    Project,
    Award,
)


class RegisterSerializer(serializers.ModelSerializer):
    # email = forms.EmailField()

    class Meta:
        model = User
        fields = ["username", "password", "first_name", "last_name"]

    # def validate_email(self, value):
    #     user = User.objects.filter(email=email)
    #     if user:
    #         raise serializers.ValidationError("Email is already taken")
    #     return BaseUserManager.normalize_email(value)

    def validate_password(self, value):
        password_validation.validate_password(value)
        return value


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "username", "first_name", "last_name"]


# class RegisterSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         fields = ["id", "username", "password"]
#         extra_kwargs = {"password": {"write_only": True}}

#         def create(self, validate_data):
#             user = User.objects.create_user(
#                 validate_data["username"],
#                 validate_data["password"],
#             )

#             return user


# class LoginSerializer(serializers.Serializer):
#     username = serializers.CharField()
#     password = serializers.CharField()

#     def validate(self, data):
#         user = authenticate(**data)
#         if user and user.is_active():
#             return user
#         raise serializers.ValidationError("Incorrect Credentials")


class ProfileSerializer(serializers.ModelSerializer):
    # other_contacts = serializers.StringRelatedField(many=True, read_only=False)

    class Meta:
        model = Profile
        fields = "__all__"
        # depth = 1


class ContactSerializer(serializers.ModelSerializer):
    class Meta:
        model = Contact
        fields = "__all__"
        # depth = 2


class OtherContactSerializer(serializers.ModelSerializer):
    class Meta:
        model = OtherContact
        fields = "__all__"
        # depth = 2


class EducBackgroundSerializer(serializers.ModelSerializer):
    class Meta:
        model = EducationalBackground
        fields = "__all__"
        # depth = 1


class SkillSerializer(serializers.ModelSerializer):
    class Meta:
        model = Skill
        fields = "__all__"
        # depth = 1


class WorkExpSerializer(serializers.ModelSerializer):
    class Meta:
        model = WorkExperience
        fields = "__all__"
        # depth = 1


class ProjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Project
        fields = "__all__"
        # depth = 1


class AwardSerializer(serializers.ModelSerializer):
    class Meta:
        model = Award
        fields = "__all__"
        # depth = 1

