from django.contrib import admin
from django.utils.html import format_html

from .models import Notice


@admin.register(Notice)
class NoticeAdmin(admin.ModelAdmin):
    list_display = ["title", "category", "priority", "date", "is_pinned", "is_published", "preview"]
    list_filter = ["category", "priority", "is_new", "is_pinned", "is_published", "date"]
    search_fields = ["title", "excerpt", "body"]
    readonly_fields = ["preview", "views", "created_at", "updated_at"]
    fieldsets = (
        ("Content", {"fields": ("title", "excerpt", "body", "image", "preview")}),
        ("Publishing", {"fields": ("category", "priority", "date", "expires_at", "is_new", "is_pinned", "is_published")}),
        ("Attachment", {"fields": ("attachment", "attachment_url", "attachment_name")}),
        ("Stats", {"fields": ("views", "created_at", "updated_at")}),
    )

    class Media:
        css = {"all": ("admin/drag-drop-upload.css",)}

    def preview(self, obj):
        if obj and obj.image:
            return format_html('<img src="{}" style="max-height:120px;border-radius:6px;" />', obj.image.url)
        return "No image uploaded"

    preview.short_description = "Image preview"
