from django import forms
from django.contrib.auth.forms import AuthenticationForm
from .models import CustomUser
from core.models import Branch

class CustomLoginForm(AuthenticationForm):
    username = forms.CharField(
        widget=forms.TextInput(attrs={
            'class': 'form-control gradient-input',
            'placeholder': 'Enter username',
            'autofocus': True
        })
    )
    password = forms.CharField(
        widget=forms.PasswordInput(attrs={
            'class': 'form-control gradient-input',
            'placeholder': 'Enter password'
        })
    )


from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
from core.models import Branch


class UserCreateForm(UserCreationForm):
    """
    Used for creating a new user with password1/password2.
    Superadmin role is not selectable.
    """
    role = forms.ChoiceField(
        choices=[(value, label) for value, label in CustomUser.ROLE_CHOICES if value != "superadmin"],
        label="Role",
    )
    branch = forms.ModelChoiceField(
        queryset=Branch.objects.filter(is_active=True),
        required=False,
        empty_label="-- Select branch (optional) --",
        label="Branch",
        
    )

    class Meta:
        model = CustomUser
        fields = [
            "username",
            "first_name",
            "last_name",
            "email",
            "phone",
            "role",
            "branch",
            "is_active",
        ]


class UserUpdateForm(UserChangeForm):
    """
    Used for editing user – no password fields here.
    Superadmin role not selectable.
    """
    password = None  # hide password field

    role = forms.ChoiceField(
        choices=[(value, label) for value, label in CustomUser.ROLE_CHOICES if value != "superadmin"],
        label="Role",
        widget=forms.TextInput(attrs={
            'class': 'form-control gradient-input',
            'placeholder': 'Enter username',
            'autofocus': True
        })
    )
    branch = forms.ModelChoiceField(
        queryset=Branch.objects.filter(is_active=True),
        required=False,
        empty_label="-- Select branch (optional) --",
        label="Branch",
    )

    class Meta:
        model = CustomUser
        fields = [
            "username",
            "first_name",
            "last_name",
            "email",
            "phone",
            "role",
            "branch",
            "is_active",
        ]

