# accounts/forms.py
from crispy_bootstrap5.bootstrap5 import FloatingField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, HTML, Column, Row, Layout
from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from .models import RegistrationSetting
from content.models import AcademicClass, StudentProfile  # uses your existing models



User = get_user_model()


class BaseSignupForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta(UserCreationForm.Meta):
        model = User
        fields = ("username", "email", "password1", "password2")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Bootstrap classes + placeholders
        self.fields["username"].widget.attrs.update({
            "class": "form-control",
            "placeholder": "choose a username",
            "autocomplete": "username",
        })
        self.fields["email"].widget.attrs.update({
            "class": "form-control",
            "placeholder": "you@example.com",
            "autocomplete": "email",
        })
        self.fields["password1"].widget.attrs.update({
            "class": "form-control",
            "placeholder": "create a password",
            "autocomplete": "new-password",
        })
        self.fields["password2"].widget.attrs.update({
            "class": "form-control",
            "placeholder": "repeat password",
            "autocomplete": "new-password",
        })


class StudentSignupForm(BaseSignupForm):
    """Use in /accounts/signup/student/"""
    pass


class StaffSignupForm(BaseSignupForm):
    """Use for teacher/admin secret signup views; set role in the view."""
    pass


class PublicLoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["username"].widget.attrs.update({
            "class": "form-control",
            "placeholder": "your username",
            "autocomplete": "username",
        })
        self.fields["password"].widget.attrs.update({
            "class": "form-control",
            "placeholder": "your password",
            "autocomplete": "current-password",
        })


class SlimAuthForm(AuthenticationForm):
    """Alternative minimal login form with Bootstrap class on all fields."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for f in self.fields.values():
            f.widget.attrs.setdefault("class", "form-control")




# ==========================================================
# ✅ FIXED StudentRegisterForm (with Invite Code)
# ==========================================================
class StudentRegisterForm(forms.Form):
    full_name    = forms.CharField(max_length=150, label="Full name")
    username     = forms.CharField(max_length=150, label="Username")
    email        = forms.EmailField(required=False, label="Email")
    password1    = forms.CharField(widget=forms.PasswordInput, label="Password")
    password2    = forms.CharField(widget=forms.PasswordInput, label="Confirm password")

    # ✅ ADD THE FIELD DECLARATION HERE
    invite_code = forms.CharField(
        max_length=50, 
        label="School Code",
        widget=forms.TextInput(attrs={'class': 'form-control', 
                                      'placeholder': 'Enter the school code provided to you'})
    )

    def clean_username(self):
        u = self.cleaned_data["username"].strip()
        if User.objects.filter(username__iexact=u).exists():
            raise ValidationError("That username is already taken.")
        return u

    # ✅ ADD THE VALIDATION METHOD
    def clean_invite_code(self):
        code_entered = self.cleaned_data.get("invite_code", "").strip()
        
        # Fetch the correct code from DB (using the model defined in accounts/models.py)
        correct_code = RegistrationSetting.get_code()
        
        if code_entered != correct_code:
            raise ValidationError("Invalid School Code. Please ask the office for the correct code.")
        
        return code_entered

    def clean(self):
        cleaned = super().clean()
        pwd1 = cleaned.get("password1")
        pwd2 = cleaned.get("password2")
        
        if pwd1 and pwd2 and pwd1 != pwd2:
            self.add_error("password2", "Passwords do not match.")
        
        return cleaned