from django import forms
from .models import Customer, User, Opportunity, Lead
from django.core.validators import validate_email

class CustomerForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = [
            'name',
            'email',
            'phone',
            'status',
            'owner',
        ]
        widgets = {
            'status': forms.Select(attrs={'class': 'form-select'}),
            'owner': forms.Select(attrs={'class': 'form-select'}),
        }

    def clean_email(self):
        email = self.cleaned_data.get('email')
        validate_email(email)
        return email


class OpportunityForm(forms.ModelForm):
    class Meta:
        model = Opportunity
        fields = [
            'name',
            'lead',
            'value',
            'stage',
            'probability',
            'close_date',
        ]
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'value': forms.NumberInput(attrs={'class': 'form-control'}),
            'probability': forms.NumberInput(attrs={'class': 'form-control'}),
            'stage': forms.Select(attrs={'class': 'form-select'}),
            'lead': forms.Select(attrs={'class': 'form-select'}),
            'close_date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
        }