"""
Forms for installment payment setup
"""

from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from .models import InstallmentSchedule


class InstallmentSetupForm(forms.Form):
    """Form for setting up installment payments"""
    
    INSTALLMENT_CHOICES = [
        (1, _('Full Payment (Today)')),
        (3, _('3 Monthly Payments')),
        (6, _('6 Monthly Payments')),
        (12, _('12 Monthly Payments')),
    ]
    
    num_installments = forms.ChoiceField(
        choices=INSTALLMENT_CHOICES,
        widget=forms.RadioSelect,
        label=_('Payment Plan'),
        help_text=_('Select your preferred payment schedule')
    )
    
    agree_terms = forms.BooleanField(
        required=True,
        label=_('I agree to the installment payment terms and conditions'),
        error_messages={
            'required': _('You must agree to the terms and conditions')
        }
    )
    
    def clean_num_installments(self):
        """Validate number of installments"""
        num = int(self.cleaned_data['num_installments'])
        
        if num < 1 or num > 12:
            raise ValidationError(_('Invalid number of installments'))
        
        return num


class InstallmentManagementForm(forms.ModelForm):
    """Form for managing installment settings (admin only)"""
    
    class Meta:
        model = InstallmentSchedule
        fields = ['auto_retry', 'max_retries']
        labels = {
            'auto_retry': _('Automatically Retry Failed Payments'),
            'max_retries': _('Maximum Retry Attempts'),
        }
        help_texts = {
            'auto_retry': _('Automatically attempt to retry failed payments'),
            'max_retries': _('How many times to retry before suspending'),
        }
    
    def clean_max_retries(self):
        """Validate max retries"""
        max_retries = self.cleaned_data['max_retries']
        
        if max_retries < 1 or max_retries > 10:
            raise ValidationError(_('Maximum retries must be between 1 and 10'))
        
        return max_retries


class UpdatePaymentMethodForm(forms.Form):
    """Form for updating payment method on installment"""
    
    confirm = forms.BooleanField(
        required=True,
        label=_('I want to update my payment method'),
        help_text=_('Your new card will be used for future installment payments')
    )
