from django import forms
from django.contrib.postgres import serializers

from .models import Enseignant, User
from django.core.exceptions import ValidationError
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth.hashers import make_password
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.forms import AuthenticationForm, UserModel
from django.utils.text import capfirst
from django.contrib.auth import (
    authenticate, get_user_model, password_validation,
)

SEXES = (("1", "Homme"), ("2", "Femme"))

class UserCreationForm(forms.ModelForm):
    """A form for creating new users. Includes all the required
    fields, plus a repeated password."""
    avatar = forms.ImageField(required=False, label="Avatar", help_text="Veillez ajouter une photo de profil", widget=forms.ImageField)
    nom = forms.CharField(max_length=255, required=True, label="Nom", help_text="Veillez entrer votre nom de famille", widget=forms.TextInput)
    prenoms = forms.CharField(max_length=255, required=True, label="Prenoms", help_text="Veillez entrer votre prenoms", widget=forms.TextInput)
    sexe = forms.ChoiceField(choices=SEXES, label="Sexe", help_text="Veillez mentionner votre sexe", widget=forms.RadioSelect, required=False)
    datenaiss = forms.DateField(required=False, label="Date de naissance", help_text="Veillez entrer votre date de naissance", widget=forms.DateInput)
    lieunaiss = forms.CharField(max_length=255, required=False, label="Lieu de naissance", help_text="Veillez entrer votre lieu de naissance", widget=forms.TextInput)
    phone = forms.CharField(max_length=255, label="Telephone", help_text="Veillez entrer votre numero de telephone", widget=forms.TextInput, required=False)
    email = forms.EmailField(max_length=255, label="Email", help_text="Veillez entrer voter adresse email", widget=forms.EmailInput, required=True)
    password1 = forms.CharField(label='Mot de passe', widget=forms.PasswordInput, required=True)
    password2 = forms.CharField(label='Confirmer mot de passe', widget=forms.PasswordInput, required=True)

    class Meta:
        model = User
        fields = "__all__"

    def clean (self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise ValidationError("Echec de confirmation du mot de passe")
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user


class UserChangeForm(forms.ModelForm):
    """A form for updating users. Includes all the fields on
    the user, but replaces the password field with admin's
    disabled password hash display field.
    """
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = User
        fields = "__all__"


class UserCheckForm(forms.Form):
    username = forms.CharField()
    secret = forms.IntegerField()

class UserRenewPasswordForm(forms.Form):
    password = forms.CharField()
    password2 = forms.CharField()




