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

from .models import SiteBranding, SiteContact


@admin.register(SiteBranding)
class SiteBrandingAdmin(admin.ModelAdmin):
    fieldsets = (
        (
            "Brand assets",
            {
                "fields": (
                    "icon",
                    "icon_alt",
                    "website_icon",
                    "hero_logo",
                    "hero_logo_alt",
                ),
                "description": (
                    "Manage each placement independently. Removing an upload restores "
                    "the built-in Tecvico asset for that placement."
                ),
            },
        ),
        (
            "Sizes",
            {
                "fields": ("navbar_icon_size", "footer_icon_size"),
                "description": (
                    "Set the logo height for each placement. Its width is calculated "
                    "automatically so the uploaded image is never cropped or stretched."
                ),
            },
        ),
        (
            "Appearance",
            {
                "fields": (
                    "show_border",
                    "border_width",
                    "border_color",
                ),
            },
        ),
    )

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

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

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


@admin.register(SiteContact)
class SiteContactAdmin(admin.ModelAdmin):
    fieldsets = (
        (
            "Email",
            {
                "fields": (
                    "support_email",
                    "email_card_title",
                    "email_card_description",
                ),
            },
        ),
        (
            "Discord",
            {
                "fields": (
                    "discord_url",
                    "discord_label",
                    "discord_card_title",
                    "discord_card_description",
                ),
            },
        ),
        (
            "Office address",
            {
                "fields": ("office_address", "office_card_title"),
                "description": "Enter one address line per row.",
            },
        ),
    )

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

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

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