from django.contrib import admin
from django.http import HttpResponseRedirect
from django.urls import reverse

from .models import (
    AboutSection,
    AboutSectionItem,
    ContactSubmission,
    DownloadItem,
    FAQEntry,
    HeroSection,
    HomepageSection,
    HomepageSectionItem,
)


@admin.register(HeroSection)
class HeroSectionAdmin(admin.ModelAdmin):
    fieldsets = (
        ("Badge", {"fields": ("badge",)}),
        ("Title", {"fields": ("title", "title_highlight")}),
        ("Text", {"fields": ("subtitle", "description")}),
        ("Primary Button", {"fields": ("primary_cta_text", "primary_cta_url")}),
        ("Secondary Button", {"fields": ("secondary_cta_text", "secondary_cta_url")}),
        ("Image", {"fields": ("image", "image_alt")}),
    )

    def has_add_permission(self, request):
        return not HeroSection.objects.exists()

    def has_delete_permission(self, request, obj=None):
        return False

    def changelist_view(self, request, extra_context=None):
        hero = HeroSection.objects.first()
        if hero:
            return HttpResponseRedirect(
                reverse("admin:pages_herosection_change", args=[hero.pk])
            )
        return super().changelist_view(request, extra_context)


class HomepageSectionItemInline(admin.TabularInline):
    model = HomepageSectionItem
    extra = 1
    fields = ("icon", "title", "content", "image", "order")
    ordering = ("order",)


@admin.register(HomepageSection)
class HomepageSectionAdmin(admin.ModelAdmin):
    list_display = ("section_type", "badge", "title", "order", "is_active")
    list_filter = ("section_type", "is_active")
    list_editable = ("order", "is_active")
    inlines = [HomepageSectionItemInline]
    fieldsets = (
        (None, {"fields": ("section_type", "badge", "title", "description")}),
        ("CTA Link (About Strip)", {"fields": ("link_text", "link_url"), "classes": ("collapse",)}),
        ("Settings", {"fields": ("order", "is_active")}),
    )


class AboutSectionItemInline(admin.StackedInline):
    model = AboutSectionItem
    extra = 1
    fields = ("badge", "title", "content", "url", "image", "image_alt", "is_featured", "order")
    ordering = ("order",)


@admin.register(AboutSection)
class AboutSectionAdmin(admin.ModelAdmin):
    list_display = ("section_type", "badge", "title", "order", "is_active")
    list_filter = ("section_type", "is_active")
    list_editable = ("order", "is_active")
    inlines = [AboutSectionItemInline]
    fieldsets = (
        (None, {"fields": ("section_type", "badge", "title", "subtitle")}),
        ("Content", {"fields": ("content_format", "content")}),
        ("Settings", {"fields": ("order", "is_active")}),
    )


@admin.register(ContactSubmission)
class ContactSubmissionAdmin(admin.ModelAdmin):
    list_display = ("name", "title", "email", "submitted_at", "is_read")
    list_filter = ("is_read", "submitted_at")
    search_fields = ("name", "title", "description", "email")
    list_editable = ("is_read",)
    readonly_fields = ("name", "title", "description", "email", "submitted_at")
    fieldsets = (
        (None, {"fields": ("name", "title", "email", "description")}),
        ("Meta", {"fields": ("submitted_at", "is_read")}),
    )


@admin.register(FAQEntry)
class FAQEntryAdmin(admin.ModelAdmin):
    list_display = ("question", "order", "is_active", "created_at")
    list_filter = ("is_active",)
    search_fields = ("question", "answer")
    list_editable = ("order", "is_active")
    fieldsets = (
        (None, {"fields": ("question",)}),
        ("Answer", {"fields": ("answer_format", "answer")}),
        ("Settings", {"fields": ("order", "is_active")}),
    )


@admin.register(DownloadItem)
class DownloadItemAdmin(admin.ModelAdmin):
    list_display = ("name", "platform", "version", "is_active", "order")
    list_filter = ("platform", "is_active")
    search_fields = ("name", "description")
    list_editable = ("order", "is_active")
    fieldsets = (
        (None, {"fields": ("name", "platform", "version", "download_url", "description")}),
        ("Settings", {"fields": ("order", "is_active")}),
    )
