from __future__ import annotations

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

from simorgh.apps.catalog.models import CatalogCategory, CatalogItem, CatalogItemVersion


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


class CatalogItemVersionInline(admin.TabularInline):
    model = CatalogItemVersion
    extra = 0
    readonly_fields = ("version", "status", "created_at")
    fields = ("version", "name", "status", "change_summary", "changed_by", "created_at")
    can_delete = False
    max_num = 0


@admin.register(CatalogItem)
class CatalogItemAdmin(admin.ModelAdmin):
    list_display = ("name", "catalog_type", "status", "category", "owner", "published_at", "tenant")
    list_filter = ("catalog_type", "status", "tenant")
    search_fields = ("name", "slug", "description", "short_description")
    prepopulated_fields = {"slug": ("name",)}
    readonly_fields = ("public_id", "created_by", "updated_by")
    inlines = [CatalogItemVersionInline]
    fieldsets = (
        (_("Basic"), {
            "fields": ("public_id", "name", "slug", "catalog_type", "category")
        }),
        (_("Description"), {
            "fields": ("short_description", "description")
        }),
        (_("Status"), {
            "fields": ("status", "owner", "published_at", "retired_at")
        }),
        (_("Media & Links"), {
            "fields": ("image", "related_items", "documentation_content_id")
        }),
        (_("AI"), {
            "fields": ("ai_description", "ai_semantic_type")
        }),
        (_("Metadata"), {
            "fields": ("created_by", "updated_by", "created_at", "updated_at")
        }),
    )
    ordering = ("tenant", "catalog_type", "-published_at", "-created_at")
