from django.contrib import admin
from audits.models import Log

class LogAdmin(admin.ModelAdmin):
    list_display = ('timestamp', 'user', 'action', 'ip_address', 'short_details')
    search_fields = ('user__username', 'action', 'details', 'ip_address')
    list_filter = ('action', 'timestamp', 'user')
    readonly_fields = ('timestamp', 'user', 'action', 'details', 'ip_address', 'user_agent')
    date_hierarchy = 'timestamp'
    
    def short_details(self, obj):
        """Display truncated details"""
        if obj.details:
            return obj.details[:50] + '...' if len(obj.details) > 50 else obj.details
        return '-'
    short_details.short_description = 'Details'
    
    def has_add_permission(self, request):
        """Disable manual adding of logs"""
        return False
    
    def has_change_permission(self, request, obj=None):
        """Disable editing of logs"""
        return False

admin.site.register(Log, LogAdmin)
