from django.contrib import admin
from .models import QivipQuiz, QivipQuestion, QivipQuizFavorite, QuestionImage, QuizCategory, QuizImage, QuizRating

# Für das QivipQuiz Modell
class QivipQuizAdmin(admin.ModelAdmin):
    list_display = ('name','published_at', 'user_id', 'status', 'category', 'creation_date', 'update_date')  # Welche Felder sollen in der Übersicht angezeigt werden
    search_fields = ('name', 'user_id__username', 'category__name')  # Suchfelder
    list_filter = ('status', 'category')  # Filteroptionen
# Für das QivipQuestion Modell
class QivipQuestionAdmin(admin.ModelAdmin):
    list_display = ('id', 'quiz_id', 'data', 'creation_date', 'update_date', 'credits')
    search_fields = ('data',)
    list_filter = ('quiz_id',)

# Für das QuizCategory Modell
class QuizCategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug', 'description')  # Zeige Name, Slug und Beschreibung an
    search_fields = ('name',)
    prepopulated_fields = {'slug': ('name',)}  # Erstelle automatisch den Slug basierend auf dem Namen



class QivipQuizFavoriteAdmin(admin.ModelAdmin):
    list_display = ('user', 'favorite', 'quiz')  # Welche Felder sollen in der Übersicht angezeigt werden


class QuizImageAdmin(admin.ModelAdmin):
     list_display = ('image',)

class QuestionImageAdmin(admin.ModelAdmin):
     list_display = ('image',)  


from django import forms
from .models import QuizCategory

class QuizCategoryForm(forms.ModelForm):
    color = forms.CharField(
        widget=forms.TextInput(attrs={'type': 'color'})
    )

    class Meta:
        model = QuizCategory
        fields = '__all__'
from django.utils.html import format_html
@admin.register(QuizCategory)
class QuizCategoryAdmin(admin.ModelAdmin):
    form = QuizCategoryForm
    list_display = ('name', 'color_preview', 'color')

    def color_preview(self, obj):
        return format_html(
            '<div style="width: 24px; height: 24px; background-color: {}; border: 2px solid #000;"></div>',
            obj.color
        )
    color_preview.short_description = "Farbe"       
        
# Registrierung der Modelle im Admin
admin.site.register(QivipQuiz, QivipQuizAdmin)
admin.site.register(QivipQuestion, QivipQuestionAdmin)
#admin.site.register(QuizCategory, QuizCategoryAdmin)
admin.site.register(QivipQuizFavorite, QivipQuizFavoriteAdmin)
admin.site.register(QuizImage, QuizImageAdmin)
admin.site.register(QuestionImage, QuestionImageAdmin)