from django import forms
from django.contrib.auth.models import User
from .models import UserProfile

class SignUpForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = UserProfile
        fields = ['email', 'password', 'phone_number',  'country', 'state', 'postal_code', 'full_address','company_name']

    def clean_email(self):
        email = self.cleaned_data['email']
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError("This email is already registered.")
        return email

    def save(self, commit=True):
        user_profile = super(SignUpForm, self).save(commit=False)
        user = User.objects.create_user(username=self.cleaned_data['email'],
                                        email=self.cleaned_data['email'],
                                        password=self.cleaned_data['password'])
        user_profile.user = user
        if commit:
            user.save()
            user_profile.save()
        return user_profile

class UserLoginForm(forms.Form):
    email = forms.EmailField(label="Email")
    password = forms.CharField(label="Password", widget=forms.PasswordInput)



class UserForm(forms.Form):
    email = forms.EmailField(label="Email")
  