from django import forms

from .models import PilotCall


class PilotCallForm(forms.ModelForm):
    class Meta:
        model = PilotCall
        fields = [
            'client_name', 'phone', 'email', 'call_source', 'ytel_audio_status', 'logics_api_status',
            'transcript', 'irs_debt', 'state_debt', 'tax_years', 'filing_status', 'employment_type',
            'occupation', 'monthly_gross_income', 'spouse_income', 'mortgage', 'rent', 'utilities',
            'car_payment', 'insurance', 'student_loans', 'credit_cards', 'assets', 'business_expenses',
            'irs_payment_plan', 'levy', 'garnishment', 'best_callback_time', 'agent_notes',
        ]
        widgets = {
            'client_name': forms.TextInput(attrs={'placeholder': 'Maria Johnson'}),
            'phone': forms.TextInput(attrs={'placeholder': '(555) 013-8842'}),
            'email': forms.EmailInput(attrs={'placeholder': 'client@example.com'}),
            'transcript': forms.Textarea(attrs={'rows': 8, 'placeholder': 'Paste the latest transcript segment from the call…'}),
            'assets': forms.Textarea(attrs={'rows': 3, 'placeholder': 'Home equity, vehicles, business assets…'}),
            'agent_notes': forms.Textarea(attrs={'rows': 4, 'placeholder': 'Summary, objections, follow-up plan…'}),
            'irs_debt': forms.NumberInput(attrs={'placeholder': '24000'}),
            'state_debt': forms.NumberInput(attrs={'placeholder': '5000'}),
            'monthly_gross_income': forms.NumberInput(attrs={'placeholder': '7200'}),
            'spouse_income': forms.NumberInput(attrs={'placeholder': '3800'}),
            'mortgage': forms.NumberInput(attrs={'placeholder': '3500'}),
            'rent': forms.NumberInput(attrs={'placeholder': '0'}),
            'utilities': forms.NumberInput(attrs={'placeholder': '450'}),
            'car_payment': forms.NumberInput(attrs={'placeholder': '620'}),
            'insurance': forms.NumberInput(attrs={'placeholder': '310'}),
            'student_loans': forms.NumberInput(attrs={'placeholder': '250'}),
            'credit_cards': forms.NumberInput(attrs={'placeholder': '900'}),
            'business_expenses': forms.NumberInput(attrs={'placeholder': '1800'}),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            base_class = 'form-check-input' if isinstance(field.widget, forms.CheckboxInput) else 'form-control'
            if isinstance(field.widget, forms.Select):
                base_class = 'form-select'
            field.widget.attrs['class'] = f"{field.widget.attrs.get('class', '')} {base_class}".strip()
        self.fields['client_name'].required = True
        self.fields['phone'].required = True
        self.fields['transcript'].required = True
