from django import forms
from django.contrib.auth.forms import AuthenticationForm

from .models import Order, OrderItem, Product


class LoginForm(AuthenticationForm):
    username = forms.CharField(
        label="Usuario",
        widget=forms.TextInput(attrs={"class": "form-control form-control-lg", "placeholder": "Tu usuario"}),
    )
    password = forms.CharField(
        label="Contraseña",
        strip=False,
        widget=forms.PasswordInput(attrs={"class": "form-control form-control-lg", "placeholder": "••••••••"}),
    )


class AddOrderItemForm(forms.ModelForm):
    class Meta:
        model = OrderItem
        fields = ["product", "quantity", "note"]
        widgets = {
            "product": forms.Select(attrs={"class": "form-select form-select-lg"}),
            "quantity": forms.NumberInput(attrs={"class": "form-control form-control-lg", "min": 1}),
            "note": forms.Textarea(attrs={"class": "form-control", "rows": 3, "placeholder": "Ej. sin cebolla, extra salsa, término medio"}),
        }
        labels = {
            "product": "Producto",
            "quantity": "Cantidad",
            "note": "Nota para cocina",
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["product"].queryset = Product.objects.filter(is_available=True)
        self.fields["quantity"].initial = 1


class OrderStatusForm(forms.Form):
    status = forms.ChoiceField(widget=forms.HiddenInput())

    def __init__(self, *args, order=None, **kwargs):
        super().__init__(*args, **kwargs)
        choices = [(status, dict(Order.Status.choices)[status]) for status in (order.allowed_transitions() if order else [])]
        self.fields["status"].choices = choices
