from django.contrib import admin
from django.contrib.admin import AdminSite
from django.utils.html import format_html
from django.urls import reverse
from .models import EditSubmission, PhotoSubmission

class ModerationAdminSite(AdminSite):
    site_header = 'ThrillWiki Moderation'
    site_title = 'ThrillWiki Moderation'
    index_title = 'Moderation Dashboard'
    
    def has_permission(self, request):
        """Only allow moderators and above to access this admin site"""
        return request.user.is_authenticated and request.user.role in ['MODERATOR', 'ADMIN', 'SUPERUSER']

moderation_site = ModerationAdminSite(name='moderation')

class EditSubmissionAdmin(admin.ModelAdmin):
    list_display = ['id', 'user_link', 'content_type', 'content_link', 'status', 'submitted_at', 'reviewed_by']
    list_filter = ['status', 'content_type', 'submitted_at']
    search_fields = ['user__username', 'reason', 'source', 'review_notes']
    readonly_fields = ['user', 'content_type', 'object_id', 'changes', 'submitted_at']
    
    def user_link(self, obj):
        url = reverse('admin:accounts_user_change', args=[obj.user.id])
        return format_html('<a href="{}">{}</a>', url, obj.user.username)
    user_link.short_description = 'User'
    
    def content_link(self, obj):
        if hasattr(obj.content_object, 'get_absolute_url'):
            url = obj.content_object.get_absolute_url()
            return format_html('<a href="{}">{}</a>', url, str(obj.content_object))
        return str(obj.content_object)
    content_link.short_description = 'Content'

    def save_model(self, request, obj, form, change):
        if 'status' in form.changed_data:
            if obj.status == 'APPROVED':
                obj.approve(request.user, obj.review_notes)
            elif obj.status == 'REJECTED':
                obj.reject(request.user, obj.review_notes)
        super().save_model(request, obj, form, change)

class PhotoSubmissionAdmin(admin.ModelAdmin):
    list_display = ['id', 'user_link', 'content_type', 'content_link', 'photo_preview', 'status', 'submitted_at', 'reviewed_by']
    list_filter = ['status', 'content_type', 'submitted_at']
    search_fields = ['user__username', 'caption', 'review_notes']
    readonly_fields = ['user', 'content_type', 'object_id', 'photo_preview', 'submitted_at']
    
    def user_link(self, obj):
        url = reverse('admin:accounts_user_change', args=[obj.user.id])
        return format_html('<a href="{}">{}</a>', url, obj.user.username)
    user_link.short_description = 'User'
    
    def content_link(self, obj):
        if hasattr(obj.content_object, 'get_absolute_url'):
            url = obj.content_object.get_absolute_url()
            return format_html('<a href="{}">{}</a>', url, str(obj.content_object))
        return str(obj.content_object)
    content_link.short_description = 'Content'
    
    def photo_preview(self, obj):
        if obj.photo:
            return format_html('<img src="{}" style="max-height: 100px; max-width: 200px;" />', obj.photo.url)
        return ''
    photo_preview.short_description = 'Photo Preview'

    def save_model(self, request, obj, form, change):
        if 'status' in form.changed_data:
            if obj.status == 'APPROVED':
                obj.approve(request.user, obj.review_notes)
            elif obj.status == 'REJECTED':
                obj.reject(request.user, obj.review_notes)
        super().save_model(request, obj, form, change)

# Register with moderation site only
moderation_site.register(EditSubmission, EditSubmissionAdmin)
moderation_site.register(PhotoSubmission, PhotoSubmissionAdmin)
