"""Content Engine — Django admin configuration."""

from __future__ import annotations

from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from simorgh.apps.content.models import ContentCategory, ContentItem, ContentVersion


class ContentVersionInline(admin.TabularInline):
    model = ContentVersion
    extra = 0
    readonly_fields = ("version", "status", "created_at")
    fields = ("version", "title", "status", "change_summary", "changed_by", "created_at")
    show_change_link = False
    can_delete = False
    max_num = 0


@admin.register(ContentCategory)
class ContentCategoryAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "parent", "is_active", "sort_order", "tenant")
    list_filter = ("is_active", "tenant")
    search_fields = ("name", "slug")
    prepopulated_fields = {"slug": ("name",)}
    ordering = ("tenant", "sort_order", "name")


@admin.register(ContentItem)
class ContentItemAdmin(admin.ModelAdmin):
    list_display = ("title", "content_type", "status", "visibility", "author", "category", "published_at", "tenant")
    list_filter = ("content_type", "status", "visibility", "tenant")
    search_fields = ("title", "slug", "body", "meta_keywords")
    prepopulated_fields = {"slug": ("title",)}
    readonly_fields = ("public_id", "created_by", "updated_by", "view_count", "helpful_count", "not_helpful_count")
    inlines = [ContentVersionInline]
    fieldsets = (
        (_("Basic"), {
            "fields": ("public_id", "title", "slug", "content_type", "category", "workspace")
        }),
        (_("Content"), {
            "fields": ("body", "excerpt")
        }),
        (_("Status & Visibility"), {
            "fields": ("status", "visibility", "author", "published_at", "archived_at")
        }),
        (_("SEO"), {
            "fields": ("meta_title", "meta_description", "meta_keywords")
        }),
        (_("AI"), {
            "fields": ("ai_description", "ai_semantic_type", "ai_summary", "related_content")
        }),
        (_("Counters"), {
            "fields": ("view_count", "helpful_count", "not_helpful_count")
        }),
        (_("Metadata"), {
            "fields": ("created_by", "updated_by", "created_at", "updated_at")
        }),
    )
    ordering = ("tenant", "-published_at", "-created_at")
