# academics/admin.py
from django.contrib import admin
from .models import SchoolClass, Subject, Attendance, ClassSubject

class ClassSubjectInline(admin.TabularInline):
    """Inline for assigning subjects/teachers directly in class admin"""
    model = ClassSubject
    extra = 2
    raw_id_fields = ['teacher']
    fields = ['subject', 'teacher', 'hours_per_week']

@admin.register(SchoolClass)
class SchoolClassAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'level', 'section', 'section_type', 'class_teacher', 'get_student_count']
    list_filter = ['level', 'section_type', 'academic_year']
    search_fields = ['name', 'code']
    inlines = [ClassSubjectInline]
    fieldsets = (
        ('Basic Information', {
            'fields': ('name', 'code', 'level', 'section', 'section_type')
        }),
        ('Academic Details', {
            'fields': ('academic_year', 'capacity', 'room_number')
        }),
        ('Teacher Assignment', {
            'fields': ('class_teacher',),
            'description': 'Select the homeroom/class teacher'
        }),
    )
    
    def get_student_count(self, obj):
        from students.models import Student
        return Student.objects.filter(current_class=obj).count()
    get_student_count.short_description = 'Students'

@admin.register(Subject)
class SubjectAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'category']
    list_filter = ['category']
    search_fields = ['name', 'code']

@admin.register(Attendance)
class AttendanceAdmin(admin.ModelAdmin):
    list_display = ['student', 'date', 'school_class', 'status', 'marked_by']
    list_filter = ['status', 'date', 'school_class']
    search_fields = ['student__first_name', 'student__last_name']
    date_hierarchy = 'date'
    raw_id_fields = ['student', 'marked_by']

@admin.register(ClassSubject)
class ClassSubjectAdmin(admin.ModelAdmin):
    """Main admin for managing teacher-subject-class assignments"""
    list_display = ['school_class', 'subject', 'teacher', 'hours_per_week']
    list_filter = ['school_class__section_type', 'subject__category', 'school_class__level']
    search_fields = ['school_class__name', 'subject__name', 'teacher__user__first_name']
    raw_id_fields = ['teacher']
    autocomplete_fields = ['school_class', 'subject']
    
    fieldsets = (
        ('Assignment', {
            'fields': ('school_class', 'subject', 'teacher')
        }),
        ('Schedule', {
            'fields': ('hours_per_week',),
            'classes': ('collapse',)
        }),
    )
    
# ---------- ADD TO BOTTOM OF academics/admin.py ----------
from academics.admin_report_cards import *   # noqa: F401, F403
# ---------------------------------------------------------
