from django.contrib import admin
from django.db.models import Q
from .models import Routine, RoutineData
from status.models import Status
from departments.models import Department
from batch.models import Batch
from teacher_management.models import Teacher
from classroom.models import Classroom
from timeslot.models import TimeSlot
from course.models import Course


class RoutineDataInline(admin.TabularInline):
    model = RoutineData
    extra = 1
    fields = ('day_of_week', 'course', 'teacher', 'room', 'time_slot', 'status')

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'status':
            kwargs['queryset'] = Status.objects.filter(
                Q(type='routine_data') & (Q(is_global=True) | Q(organization__isnull=False))
            )
        return super().formfield_for_foreignkey(db_field, request, **kwargs)


@admin.register(Routine)
class RoutineAdmin(admin.ModelAdmin):
    list_display = ('name', 'organization', 'batch', 'semester', 'status', 'created_at', 'updated_at')
    list_filter = ('organization', 'status', 'batch', 'semester')
    search_fields = ('name', 'batch__batch_name', 'semester__title')
    prepopulated_fields = {'slug': ('name',)}
    readonly_fields = ('id', 'created_at', 'updated_at')
    exclude = ('is_global',)
    inlines = [RoutineDataInline]

    def save_model(self, request, obj, form, change):
        obj.is_global = False
        super().save_model(request, obj, form, change)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'department':
            kwargs['queryset'] = Department.objects.filter(
                Q(is_global=True) | Q(organization__isnull=False)
            )
        elif db_field.name == 'batch':
            kwargs['queryset'] = Batch.objects.filter(
                Q(is_global=True) | Q(organization__isnull=False)
            )
        elif db_field.name == 'status':
            kwargs['queryset'] = Status.objects.filter(
                Q(type='routine') & (Q(is_global=True) | Q(organization__isnull=False))
            )
        return super().formfield_for_foreignkey(db_field, request, **kwargs)


@admin.register(RoutineData)
class RoutineDataAdmin(admin.ModelAdmin):
    list_display = ('routine', 'day_of_week', 'course', 'teacher', 'room', 'time_slot', 'status', 'created_at', 'updated_at')
    list_filter = ('day_of_week', 'routine__batch', 'routine__semester', 'time_slot')
    search_fields = ('routine__name', 'course__name', 'teacher__first_name', 'teacher__last_name', 'room__room_number')
    readonly_fields = ('id', 'created_at', 'updated_at')
    exclude = ('is_global', 'organization')

    def save_model(self, request, obj, form, change):
        obj.is_global = False
        if not obj.organization_id and obj.routine_id:
            obj.organization = obj.routine.organization
        super().save_model(request, obj, form, change)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'status':
            kwargs['queryset'] = Status.objects.filter(
                Q(type='routine_data') & (Q(is_global=True) | Q(organization__isnull=False))
            )
        return super().formfield_for_foreignkey(db_field, request, **kwargs)
