from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.utils.html import format_html
from .models import SlugHistory

@admin.register(SlugHistory)
class SlugHistoryAdmin(admin.ModelAdmin):
    list_display = ['content_object_link', 'old_slug', 'created_at']
    list_filter = ['content_type', 'created_at']
    search_fields = ['old_slug', 'object_id']
    readonly_fields = ['content_type', 'object_id', 'old_slug', 'created_at']
    date_hierarchy = 'created_at'
    ordering = ['-created_at']

    def content_object_link(self, obj):
        """Create a link to the related object's admin page"""
        try:
            url = obj.content_object.get_absolute_url()
            return format_html(
                '<a href="{}">{}</a>',
                url,
                str(obj.content_object)
            )
        except (AttributeError, ValueError):
            return str(obj.content_object)
    content_object_link.short_description = 'Object'

    def has_add_permission(self, request):
        """Disable manual creation of slug history records"""
        return False

    def has_change_permission(self, request, obj=None):
        """Disable editing of slug history records"""
        return False
