"""
Forms for deposits app.
"""
from django import forms
from referrals.services import get_maximum_qualifying_deposit, get_minimum_qualifying_deposit
from .models import Deposit, PaymentInfo


class DepositForm(forms.ModelForm):
    """
    Form for submitting deposit requests.
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        minimum_deposit = get_minimum_qualifying_deposit()
        maximum_deposit = get_maximum_qualifying_deposit()
        self.fields['amount'].widget.attrs.update({
            'min': str(minimum_deposit),
            'max': str(maximum_deposit),
        })

    class Meta:
        model = Deposit
        fields = ('amount', 'payment_method', 'transaction_id', 'wallet_address', 'screenshot', 'notes')
        widgets = {
            'amount': forms.NumberInput(attrs={
                'class': 'form-control',
                'step': '0.01',
                'placeholder': 'Enter deposit amount'
            }),
            'payment_method': forms.Select(attrs={'class': 'form-select'}),
            'transaction_id': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Transaction ID / Hash'
            }),
            'wallet_address': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Your wallet address (where you sent from)'
            }),
            'screenshot': forms.FileInput(attrs={'class': 'form-control'}),
            'notes': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 2,
                'placeholder': 'Additional notes (optional)'
            }),
        }

    def clean_amount(self):
        amount = self.cleaned_data.get('amount')
        minimum_deposit = get_minimum_qualifying_deposit()
        maximum_deposit = get_maximum_qualifying_deposit()

        if amount is None:
            return amount

        if amount < minimum_deposit:
            raise forms.ValidationError(f'Minimum deposit amount is ${minimum_deposit}.')

        if amount > maximum_deposit:
            raise forms.ValidationError(f'Maximum deposit amount is ${maximum_deposit}.')

        return amount


class PaymentInfoForm(forms.ModelForm):
    """
    Form for creating/editing payment information.
    """
    class Meta:
        model = PaymentInfo
        fields = ('payment_method', 'wallet_address', 'qr_code', 'instructions', 'is_active', 'minimum_amount')
        widgets = {
            'payment_method': forms.Select(attrs={'class': 'form-select'}),
            'wallet_address': forms.TextInput(attrs={'class': 'form-control'}),
            'qr_code': forms.FileInput(attrs={'class': 'form-control'}),
            'instructions': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'is_active': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
            'minimum_amount': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}),
        }


class DepositVerificationForm(forms.ModelForm):
    """
    Form for admin to verify deposits.
    """
    class Meta:
        model = Deposit
        fields = ('status', 'verification_notes')
        widgets = {
            'status': forms.Select(attrs={'class': 'form-select'}),
            'verification_notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }
