from django.contrib.admin import register
from django.db.models import TextField
from django_summernote.admin import (
    SummernoteModelAdminMixin,
)
from unfold.admin import ModelAdmin
from unfold_markdown import MarkdownWidget

from engine.blog.models import Post, PostTag
from engine.core.admin import ActivationActionsMixin, FieldsetsMixin


@register(Post)
class PostAdmin(
    SummernoteModelAdminMixin, FieldsetsMixin, ActivationActionsMixin, ModelAdmin
):
    list_display = ("title", "author", "slug", "created", "modified")
    list_filter = ("author", "tags", "created", "modified")
    search_fields = ("title", "content", "slug")
    filter_horizontal = ("tags",)
    date_hierarchy = "created"
    autocomplete_fields = ("tags",)
    formfield_overrides = {TextField: {"widget": MarkdownWidget}}
    readonly_fields = (
        "uuid",
        "slug",
        "modified",
        "created",
    )

    summernote_fields = ("content",)
    general_fields = [
        "title",
        "author",
        "content",
        "file",
    ]
    relation_fields = [
        "author",
        "tags",
    ]


@register(PostTag)
class PostTagAdmin(ModelAdmin):
    list_display = ("tag_name", "name")
    search_fields = ("tag_name", "name")
    ordering = ("tag_name",)
