from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.core.exceptions import ValidationError
from django.forms import ModelForm
from common.models import UserData

class User_creation_form(forms.Form):
    first_name = forms.CharField(label='Enter First Name',min_length=1,max_length=150,widget=forms.TextInput(attrs={'class': 'form-control'}))
    last_name = forms.CharField(label='Enter Last Name',min_length=1,max_length=150,widget=forms.TextInput(attrs={'class': 'form-control'}))
    username = forms.CharField(label='Enter Username', min_length=4, max_length=150,widget=forms.TextInput(attrs={'class': 'form-control'}))
    email = forms.EmailField(label='Enter email',widget=forms.EmailInput(attrs={'class': 'form-control'}))
    password1 = forms.CharField(label='Enter password', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
 
    def clean_username(self):
        username = self.cleaned_data['username'].lower()
        r = User.objects.filter(username=username)
        if r.count():
            raise  ValidationError("Username already exists")
        return username
 
    def clean_email(self):
        email = self.cleaned_data['email'].lower()
        r = User.objects.filter(email=email)
        if r.count():
            raise  ValidationError("Email already exists")
        return email
 
    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
 
        if password1 and password2 and password1 != password2:
            raise ValidationError("Password don't match")
 
        return password2
 
    def save(self, commit=True):
        user = User.objects.create_user(
            self.cleaned_data['username'],
            self.cleaned_data['email'],
            self.cleaned_data['password1']
        )
        user.first_name = self.cleaned_data['first_name'].lower()
        user.last_name = self.cleaned_data['last_name'].lower()
        user.save()
        return user

class Signup_form(ModelForm):
    class Meta:
        model = UserData
        fields = ['dob','ph_no']
        widgets = {
        'dob': forms.DateInput(format=('%d/%m/%Y'), attrs={'class':'form-control', 'placeholder':'Select a date', 'type':'date'}),
        'ph_no':forms.NumberInput(attrs={'class':'form-control', 'placeholder':'Enter your phone number'}),
    }