from dajngo import forms
from django.contrib.auth import forms as auth_forms


class BootstrapMixin:
    required_css_class = "required"

    widget_classes = {
        "checkbox": "form-check-input",
        "select": "form-select",
    }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            if getattr(field.widget, "_bs_enabled", False):
                continue
            widgets = getattr(field.widget, "widgets", [field.widget])
            for widget in widgets:
                input_type = getattr(widget, "input_type", "")
                class_name = self.widget_classes.get(input_type, "form-control")
                if "class" in widget.attrs:
                    widget.attrs["class"] += " " + class_name
                else:
                    widget.attrs.update({"class": class_name})


class LoginForm(BootstrapMixin, auth_forms.AuthenticationForm):
    username = forms.EmailField(
        widget=forms.EmailInput(attrs={"autofocus": True}),
    )
