import re
from django import forms
from .models import ContactRequest


class ContactForm(forms.ModelForm):
    class Meta:
        model  = ContactRequest
        fields = ['last_name', 'first_name', 'email', 'phone', 'message']
        widgets = {
            'last_name':  forms.TextInput(attrs={'placeholder': 'Nom',            'class': 'cta-input'}),
            'first_name': forms.TextInput(attrs={'placeholder': 'Prénom',         'class': 'cta-input'}),
            'email':      forms.EmailInput(attrs={'placeholder': 'Adresse email', 'class': 'cta-input'}),
            'phone':      forms.TextInput(attrs={'placeholder': 'Téléphone',      'class': 'cta-input', 'inputmode': 'numeric', 'pattern': '[0-9+ ]{6,20}'}),
            'message':    forms.Textarea(attrs={'placeholder': 'Votre demande…',  'class': 'cta-input cta-textarea', 'rows': 4}),
        }
        labels = {
            'last_name':  '',
            'first_name': '',
            'email':      '',
            'phone':      '',
            'message':    '',
        }

    def clean_phone(self):
        phone = self.cleaned_data.get('phone', '').strip()
        if phone and not re.fullmatch(r'[0-9+\s\-]{6,20}', phone):
            raise forms.ValidationError('Numéro invalide — chiffres uniquement.')
        return phone
