from django.contrib import admin
from .models import PollSurvey, Question, Response, ResponseDetail,Option



# Inlines for nested editing in the admin panel
class QuestionInline(admin.TabularInline):
    model = Question
    extra = 1

class ResponseDetailInline(admin.TabularInline):
    model = ResponseDetail
    extra = 1

class OptionInline(admin.TabularInline):
    model = Option
    extra = 1

@admin.register(PollSurvey)
class PollSurveyAdmin(admin.ModelAdmin):
    list_display = ('title', 'tenant', 'type', 'status', 'start_date', 'end_date',"id")
    list_filter = ('tenant', 'type', 'status')
    search_fields = ('title', 'description')
    inlines = [QuestionInline]
    readonly_fields = ('created_at', 'updated_at')

@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
    list_display = ('question_text', 'survey', 'question_type', 'order')
    list_filter = ('survey', 'question_type')
    search_fields = ('question_text',)
    ordering = ('survey', 'order')
    inlines = [OptionInline]

@admin.register(Response)
class ResponseAdmin(admin.ModelAdmin):
    list_display = ('user', 'survey', 'submitted_at')
    list_filter = ('survey', 'submitted_at')
    search_fields = ('user__username',)
    inlines = [ResponseDetailInline]
    readonly_fields = ('submitted_at',)

@admin.register(ResponseDetail)
class ResponseDetailAdmin(admin.ModelAdmin):
    list_display = ('response', 'question', 'answer_text', 'option', 'rating_value', 'id')
    list_filter = ('response__survey',)
    readonly_fields = ('id',)


