# banking/forms.py
from django import forms
from django.forms import ModelChoiceField
from django.utils.translation import gettext_lazy as _
from .models.account import Account
from .models.transaction import Transaction


class AccountChoiceField(ModelChoiceField):
    def label_from_instance(self, account):
        return f"{account.iban} - {_('Balance')}: {account.balance:.2f}"


class NewTransactionForm(forms.ModelForm):
    amount = forms.FloatField(min_value=0.01, label=_("Amount"))

    beneficiary_iban = forms.CharField(
        label=_("Beneficiary"),
        max_length=24,
    )

    information = forms.CharField(
        label=_("Information for beneficiary"),
        required=False,
        widget=forms.Textarea(attrs={"rows": "2"}),
    )

    payer_iban = AccountChoiceField(label=_("Payer IBAN"), queryset=None)

    def __init__(self, user, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["payer_iban"].queryset = Account.objects.filter(user=user)

        self.fields["amount"].widget.attrs["placeholder"] = _("Amount")
        self.fields["beneficiary_iban"].widget.attrs["placeholder"] = _("Beneficiary")
        self.fields["information"].widget.attrs["placeholder"] = _("Information for beneficiary")

    class Meta:
        model = Transaction
        fields = [
            "payer_iban",
            "amount",
            "beneficiary_iban",
            "information",
            "variable_symbol",
            "specific_symbol",
            "constant_symbol",
        ]
        labels = {
            "variable_symbol": _("Variable symbol"),
            "specific_symbol": _("Specific symbol"),
            "constant_symbol": _("Constant symbol"),
        }


class PaymentVerificationForm(forms.Form):
    code = forms.CharField(
        label=_("Verification code"),
        max_length=6,
        min_length=6,
        widget=forms.TextInput(
            attrs={
                "class": "form-control text-center",
                "inputmode": "numeric",
                "autocomplete": "one-time-code",
            }
        ),
    )
