from django import forms
from django.core.validators import FileExtensionValidator
from .models import JobApplication


class JobApplicationForm(forms.ModelForm):
    cv_file = forms.FileField(
        validators=[FileExtensionValidator(allowed_extensions=['pdf', 'doc', 'docx'])],
        widget=forms.ClearableFileInput(attrs={
            'accept': '.pdf,.doc,.docx',
            'class': 'form-file-input',
        }),
        label='CV (PDF ou Word)',
        help_text='Formats acceptés : PDF, DOC, DOCX — taille maximum 5 Mo',
    )

    class Meta:
        model = JobApplication
        fields = [
            'first_name', 'last_name', 'email', 'phone',
            'linkedin_url', 'cover_letter', 'cv_file', 'portfolio_url',
        ]
        labels = {
            'first_name':    'Prénom',
            'last_name':     'Nom de famille',
            'email':         'Adresse email',
            'phone':         'Téléphone (optionnel)',
            'linkedin_url':  'LinkedIn (optionnel)',
            'cover_letter':  'Lettre de motivation',
            'portfolio_url': 'Portfolio / GitHub (optionnel)',
        }
        widgets = {
            'first_name':    forms.TextInput(attrs={'placeholder': 'Votre prénom', 'class': 'form-input'}),
            'last_name':     forms.TextInput(attrs={'placeholder': 'Votre nom', 'class': 'form-input'}),
            'email':         forms.EmailInput(attrs={'placeholder': 'vous@exemple.com', 'class': 'form-input'}),
            'phone':         forms.TextInput(attrs={'placeholder': '+225 07 00 00 00 00', 'class': 'form-input'}),
            'linkedin_url':  forms.URLInput(attrs={'placeholder': 'https://linkedin.com/in/...', 'class': 'form-input'}),
            'cover_letter':  forms.Textarea(attrs={
                'rows': 8,
                'class': 'form-textarea',
                'placeholder': 'Décrivez votre motivation et vos atouts pour ce poste...',
            }),
            'portfolio_url': forms.URLInput(attrs={'placeholder': 'https://github.com/...', 'class': 'form-input'}),
        }

    def clean_cv_file(self):
        cv = self.cleaned_data.get('cv_file')
        if cv and cv.size > 5 * 1024 * 1024:
            raise forms.ValidationError('Le fichier CV ne peut pas dépasser 5 Mo.')
        return cv
