from django import forms
from .models import Registration, PaymentProof, Event


class VerifyCodeForm(forms.Form):
    registration_code = forms.CharField(
        max_length=5,
        min_length=5,
        label="Registration Code",
        widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': 'Enter your 5-digit registration code',
        })
    )


class EventSelectionForm(forms.Form):
    event = forms.ModelChoiceField(
        queryset=Event.objects.filter(is_active=True),
        empty_label="Select an event",
        label="Event",
        widget=forms.Select(attrs={'class': 'form-select'})
    )


class RegistrationForm(forms.ModelForm):
    class Meta:
        model = Registration
        fields = ['full_name', 'email', 'organisation']
        widgets = {
            'full_name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Enter your full name',
            }),
            'email': forms.EmailInput(attrs={
                'class': 'form-control',
                'placeholder': 'Enter your email address',
            }),
            'organisation': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Enter your organisation or company name',
            }),
        }


class PaymentProofForm(forms.ModelForm):
    class Meta:
        model = PaymentProof
        fields = ['file']
        widgets = {
            'file': forms.FileInput(attrs={
                'class': 'form-control',
                'accept': 'image/*,.pdf',
            }),
        }
        
    def clean_file(self):
        file = self.cleaned_data.get('file')
        if file:
            # Check file size (limit to 5MB)
            if file.size > 5 * 1024 * 1024:
                raise forms.ValidationError("File size cannot exceed 5MB.")
            
            # Check file type
            allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'application/pdf']
            if file.content_type not in allowed_types:
                raise forms.ValidationError("Only JPEG, PNG, GIF, or PDF files are allowed.")
        
        return file
