from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import BaseUser

class BaseUserForm(UserCreationForm):
    class Meta:
        model = BaseUser
        fields = [
            'first_name',
            'last_name',
            'bio',
            'address',
            'contact_number',
            'image',
            'citizenship',
            'certificate',
            'price',
            'role',
            'email',
        ]
    password1 = forms.CharField(
        widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'}),
        label='Password',
        min_length=8,
    )
    password2 = forms.CharField(
        widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Confirm Password'}),
        label='Confirm Password',
        min_length=8,
    )

    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')

        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match.")
        return password2

    def save(self, commit=True):
        user = super().save(commit=False)
        user.set_password(self.cleaned_data['password1'])
        if commit:
            user.save()
        return user
