from django import forms
from .models import Donation

class DonationForm(forms.ModelForm):
    class Meta:
        model = Donation
        fields = ['full_name', 'email', 'amount', 'custom_amount', 'proof_of_payment', 'message', 'is_anonymous']
        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'
            }),
            'amount': forms.Select(attrs={
                'class': 'form-select',
                'id': 'amountSelect'
            }),
            'custom_amount': forms.NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'Enter custom amount',
                'id': 'customAmount',
                'min': '0',
                'step': '0.01'
            }),
            'proof_of_payment': forms.FileInput(attrs={
                'class': 'form-control',
                'accept': 'image/*,.pdf'
            }),
            'message': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': '4',
                'placeholder': 'Leave a message (optional)'
            }),
            'is_anonymous': forms.CheckboxInput(attrs={
                'class': 'form-check-input'
            })
        }
        labels = {
            'is_anonymous': 'Make this donation anonymous',
            'proof_of_payment': 'Proof of Payment (Image or PDF)',
            'custom_amount': 'Custom Amount (if Other selected)'
        }
        help_texts = {
            'proof_of_payment': 'Maximum file size: 5MB',
            'custom_amount': 'Enter amount in Zambian Kwacha (ZMW)'
        }
        
    def clean(self):
        cleaned_data = super().clean()
        amount = cleaned_data.get('amount')
        custom_amount = cleaned_data.get('custom_amount')
        
        if amount == 'other' and not custom_amount:
            raise forms.ValidationError("Please specify the custom amount.")
            
        if 'proof_of_payment' in self.files:
            file = self.files['proof_of_payment']
            if file.size > 5 * 1024 * 1024:  # 5MB
                raise forms.ValidationError("File size must be less than 5MB")
        
        return cleaned_data