# finance/forms.py
from django import forms
from .models import (
    FeeStructure, SchoolItemPrice, StudentEnrollmentFee,
    Invoice, Payment, Expense
)


# ---------------------------------------------------------------------------
# FeeStructure form — matches the new model fields exactly
# ---------------------------------------------------------------------------

class FeeStructureForm(forms.ModelForm):
    class Meta:
        model = FeeStructure
        fields = [
            'section', 'class_group', 'academic_year',
            'registration_new', 'registration_old',
            'total_fees', 'installment_1', 'installment_2',
            'specific_classes', 'is_active', 'description',
        ]
        widgets = {
            'section':          forms.Select(attrs={'class': 'form-select'}),
            'class_group':      forms.Select(attrs={'class': 'form-select'}),
            'academic_year':    forms.TextInput(attrs={'class': 'form-control'}),
            'registration_new': forms.NumberInput(attrs={'class': 'form-control'}),
            'registration_old': forms.NumberInput(attrs={'class': 'form-control'}),
            'total_fees':       forms.NumberInput(attrs={'class': 'form-control'}),
            'installment_1':    forms.NumberInput(attrs={'class': 'form-control'}),
            'installment_2':    forms.NumberInput(attrs={'class': 'form-control'}),
            'description':      forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }
        help_texts = {
            'total_fees':    'Must equal Installment 1 + Installment 2.',
            'class_group':   'Select the row from the fee table this structure represents.',
        }

    def clean(self):
        cleaned = super().clean()
        total   = cleaned.get('total_fees')
        inst1   = cleaned.get('installment_1')
        inst2   = cleaned.get('installment_2')
        if total and inst1 and inst2:
            if inst1 + inst2 != total:
                raise forms.ValidationError(
                    f'Installment 1 ({inst1:,}) + Installment 2 ({inst2:,}) '
                    f'must equal Total Fees ({total:,}).'
                )
        return cleaned


# ---------------------------------------------------------------------------
# SchoolItemPrice form — uniforms, sportswear, report card, pullovers
# ---------------------------------------------------------------------------

class SchoolItemPriceForm(forms.ModelForm):
    class Meta:
        model = SchoolItemPrice
        fields = ['item_type', 'name', 'price', 'academic_year', 'is_active', 'description']
        widgets = {
            'item_type':    forms.Select(attrs={'class': 'form-select'}),
            'name':         forms.TextInput(attrs={'class': 'form-control'}),
            'price':        forms.NumberInput(attrs={'class': 'form-control'}),
            'academic_year': forms.TextInput(attrs={'class': 'form-control'}),
            'description':  forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
        }


# ---------------------------------------------------------------------------
# StudentEnrollmentFee form
# ---------------------------------------------------------------------------

class StudentEnrollmentFeeForm(forms.ModelForm):
    class Meta:
        model = StudentEnrollmentFee
        fields = [
            'student', 'fee_structure', 'academic_year',
            'is_new_pupil', 'installment_1_due', 'installment_2_due', 'notes',
        ]
        widgets = {
            'student':          forms.Select(attrs={'class': 'form-select'}),
            'fee_structure':    forms.Select(attrs={'class': 'form-select'}),
            'academic_year':    forms.TextInput(attrs={'class': 'form-control'}),
            'installment_1_due': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}),
            'installment_2_due': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}),
            'notes':            forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
        }
        help_texts = {
            'is_new_pupil': 'Tick for new admissions (reg. fee = 15,000 XAF). '
                            'Untick for returning pupils (reg. fee = 10,000 XAF).',
        }


# ---------------------------------------------------------------------------
# Invoice form — uses invoice_type instead of term
# ---------------------------------------------------------------------------

class InvoiceForm(forms.ModelForm):
    class Meta:
        model = Invoice
        fields = [
            'student', 'academic_year', 'invoice_type',
            'enrollment_fee', 'due_date', 'notes',
        ]
        widgets = {
            'student':       forms.Select(attrs={'class': 'form-select'}),
            'academic_year': forms.TextInput(attrs={'class': 'form-control'}),
            'invoice_type':  forms.Select(attrs={'class': 'form-select'}),
            'enrollment_fee': forms.Select(attrs={'class': 'form-select'}),
            'due_date':      forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}),
            'notes':         forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }


# ---------------------------------------------------------------------------
# Payment form — unchanged except cheque_number replaces check
# ---------------------------------------------------------------------------

class PaymentForm(forms.ModelForm):
    class Meta:
        model = Payment
        fields = [
            'invoice', 'amount', 'payment_method', 'payment_date',
            'mobile_money_provider', 'mobile_money_number',
            'transaction_id', 'bank_name', 'cheque_number', 'notes',
        ]
        widgets = {
            'invoice':               forms.Select(attrs={'class': 'form-select'}),
            'amount':                forms.NumberInput(attrs={'class': 'form-control'}),
            'payment_method':        forms.Select(attrs={'class': 'form-select'}),
            'payment_date':          forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}),
            'mobile_money_provider': forms.Select(attrs={'class': 'form-select'}),
            'mobile_money_number':   forms.TextInput(attrs={'class': 'form-control'}),
            'transaction_id':        forms.TextInput(attrs={'class': 'form-control'}),
            'bank_name':             forms.TextInput(attrs={'class': 'form-control'}),
            'cheque_number':         forms.TextInput(attrs={'class': 'form-control'}),
            'notes':                 forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
        }


# ---------------------------------------------------------------------------
# Expense form — no changes needed
# ---------------------------------------------------------------------------

class ExpenseForm(forms.ModelForm):
    class Meta:
        model = Expense
        fields = [
            'category', 'description', 'amount', 'expense_date',
            'payment_method', 'vendor', 'notes', 'receipt_file',
        ]
        widgets = {
            'category':     forms.Select(attrs={'class': 'form-select'}),
            'description':  forms.TextInput(attrs={'class': 'form-control'}),
            'amount':       forms.NumberInput(attrs={'class': 'form-control'}),
            'expense_date': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}),
            'payment_method': forms.Select(attrs={'class': 'form-select'}),
            'vendor':       forms.TextInput(attrs={'class': 'form-control'}),
            'notes':        forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
        }