from django.contrib import admin
from .models import Course, Module, Lesson, Comment


class LessonInline(admin.TabularInline):
    model = Lesson
    extra = 0
    fields = ("name", "slug", "is_premium", "order")
    ordering = ("order",)
    prepopulated_fields = {"slug": ("name",)}


class ModuleInline(admin.TabularInline):
    model = Module
    extra = 0
    fields = ("name", "slug", "enable", "order")
    ordering = ("order",)
    prepopulated_fields = {"slug": ("name",)}


@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "author", "enable", "created_at", "updated_at")
    list_filter = ("enable", "author", "created_at", "updated_at")
    search_fields = ("name", "tags", "description", "author__username")
    prepopulated_fields = {"slug": ("name",)}
    readonly_fields = ("created_at", "updated_at")
    inlines = [ModuleInline]


@admin.register(Module)
class ModuleAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "course", "enable", "order", "created_at", "updated_at")
    list_filter = ("enable", "course", "created_at", "updated_at")
    search_fields = ("name", "description", "course__name")
    prepopulated_fields = {"slug": ("name",)}
    readonly_fields = ("created_at", "updated_at")
    ordering = ("course", "order")
    inlines = [LessonInline]


@admin.register(Lesson)
class LessonAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "module", "is_premium", "order")
    list_filter = ("is_premium", "module")
    search_fields = ("name", "content", "module__name")
    prepopulated_fields = {"slug": ("name",)}
    ordering = ("module", "order")


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ("lesson", "user", "created_at", "is_active")
    list_filter = ("is_active", "created_at")
    search_fields = ("content", "user__username", "lesson__name")
    readonly_fields = ("created_at", "updated_at")