from django import forms
from django.contrib.auth.models import User
from .models import Student, Subject, Classroom, City, Governorate, Teacher, Resource

class StudentRegistrationForm(forms.ModelForm):
    full_name = forms.CharField(max_length=150, required=True, label="الاسم الكامل")
    username = forms.CharField(max_length=150, required=True, label="اسم المستخدم")
    email = forms.EmailField(required=True, label="البريد الإلكتروني")
    password = forms.CharField(widget=forms.PasswordInput, required=True, label="كلمة المرور")
    password_confirm = forms.CharField(widget=forms.PasswordInput, required=True, label="تأكيد كلمة المرور")
    
    classroom = forms.ModelChoiceField(queryset=Classroom.objects.all(), required=True, empty_label="اختر الصف", label="الصف الدراسي")
    subjects = forms.ModelMultipleChoiceField(
        queryset=Subject.objects.all(), 
        required=False, 
        widget=forms.CheckboxSelectMultiple,
        label="اختر المواد"
    )

    class Meta:
        model = Student
        fields = ['mobile_number', 'governorate', 'city', 'avatar']
        labels = {
            'mobile_number': 'رقم الهاتف',
            'governorate': 'المحافظة',
            'city': 'المدينة',
            'avatar': 'الصورة الشخصية'
        }
        widgets = {
            'avatar': forms.FileInput(attrs={'accept': 'image/*', 'capture': 'camera'}), 
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
        # Add Bootstrap classes
        for field_name, field in self.fields.items():
            if field.widget.__class__.__name__ == 'CheckboxInput':
                 field.widget.attrs['class'] = 'form-check-input'
            elif field.widget.__class__.__name__ != 'CheckboxSelectMultiple':
                 field.widget.attrs['class'] = 'form-control'
                 field.widget.attrs['placeholder'] = field.label  # Important for Floating Labels!

        # optimize subjects loading
        self.fields['subjects'].queryset = Subject.objects.none()

        if 'classroom' in self.data:
            try:
                classroom_id = int(self.data.get('classroom'))
                self.fields['subjects'].queryset = Subject.objects.filter(classroom_id=classroom_id)
            except (ValueError, TypeError):
                pass
        elif self.instance.pk and self.instance.classroom:
            self.fields['subjects'].queryset = Subject.objects.filter(classroom=self.instance.classroom)

        # optimize city loading
        self.fields['city'].queryset = City.objects.none()

        if 'governorate' in self.data:
            try:
                governorate_id = int(self.data.get('governorate'))
                self.fields['city'].queryset = City.objects.filter(governorate_id=governorate_id)
            except (ValueError, TypeError):
                pass
        elif self.instance.pk and self.instance.governorate:
            self.fields['city'].queryset = City.objects.filter(governorate=self.instance.governorate)

    def clean(self):
        cleaned_data = super().clean()
        password = cleaned_data.get("password")
        password_confirm = cleaned_data.get("password_confirm")

        if password and password_confirm and password != password_confirm:
            self.add_error('password_confirm', "كلمات المرور غير متطابقة")

        return cleaned_data

    def save(self, commit=True):
        full_name = self.cleaned_data['full_name']
        if " " in full_name:
            first_name, last_name = full_name.split(" ", 1)
        else:
            first_name = full_name
            last_name = ""

        user = User.objects.create_user(
            username=self.cleaned_data['username'],
            email=self.cleaned_data['email'],
            password=self.cleaned_data['password'],
            first_name=first_name,
            last_name=last_name
        )
        student = super().save(commit=False)
        student.user = user
        student.classroom = self.cleaned_data['classroom']
        
        if commit:
            student.save()
            student.subscribed_subjects.set(self.cleaned_data['subjects'])
        return student

class TeacherProfileForm(forms.ModelForm):
    first_name = forms.CharField(max_length=30, required=True, label="الاسم الأول")
    last_name = forms.CharField(max_length=150, required=True, label="الاسم الأخير")
    email = forms.EmailField(required=True, label="البريد الإلكتروني")

    class Meta:
        model = Teacher
        fields = ['bio', 'specialization', 'avatar']
        labels = {
            'bio': 'نبذة عني',
            'specialization': 'التخصص',
            'avatar': 'الصورة الشخصية'
        }
        widgets = {
            'bio': forms.Textarea(attrs={'rows': 4}),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if self.instance and self.instance.user:
            self.fields['first_name'].initial = self.instance.user.first_name
            self.fields['last_name'].initial = self.instance.user.last_name
            self.fields['email'].initial = self.instance.user.email
            
        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'

    def save(self, commit=True):
        teacher = super().save(commit=False)
        if commit:
            user = teacher.user
            user.first_name = self.cleaned_data['first_name']
            user.last_name = self.cleaned_data['last_name']
            user.email = self.cleaned_data['email']
            user.save()
            teacher.save()
        return teacher

class ResourceForm(forms.ModelForm):
    class Meta:
        model = Resource
        fields = ['title_ar', 'title_en', 'resource_type', 'file', 'link']
        labels = {
            'title_ar': 'العنوان (عربي)',
            'title_en': 'العنوان (إنجليزي)',
            'resource_type': 'نوع المصدر',
            'file': 'الملف',
            'link': 'الرابط'
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
