from django.contrib import admin
from django.utils.html import format_html
from .models import Review, ReviewImage, ReviewLike, ReviewReport

class ReviewImageInline(admin.TabularInline):
    model = ReviewImage
    extra = 1
    fields = ('image', 'caption', 'order')

@admin.register(Review)
class ReviewAdmin(admin.ModelAdmin):
    list_display = ('get_title', 'user', 'rating', 'created_at', 'is_published', 'get_reports_count')
    list_filter = ('is_published', 'rating', 'created_at')
    search_fields = ('user__username', 'content', 'title')
    readonly_fields = ('created_at', 'updated_at')
    actions = ['publish_reviews', 'unpublish_reviews']
    inlines = [ReviewImageInline]
    
    fieldsets = (
        ('Review Details', {
            'fields': (('user', 'rating'), 'title', 'content')
        }),
        ('Review Target', {
            'fields': (('content_type', 'object_id'),)
        }),
        ('Moderation', {
            'fields': ('is_published', 'moderation_notes', 'moderated_by', 'moderated_at')
        }),
        ('Metadata', {
            'fields': ('created_at', 'updated_at', 'visit_date'),
            'classes': ('collapse',)
        }),
    )

    def get_title(self, obj):
        return f"Review of {obj.content_object}"
    get_title.short_description = 'Review Title'

    def get_reports_count(self, obj):
        count = obj.reports.filter(resolved=False).count()
        if count > 0:
            return format_html(
                '<span style="color: {};">{}</span>',
                'red' if count > 2 else 'orange',
                count
            )
        return count
    get_reports_count.short_description = 'Reports'

    def publish_reviews(self, request, queryset):
        queryset.update(is_published=True)
    publish_reviews.short_description = "Publish selected reviews"

    def unpublish_reviews(self, request, queryset):
        queryset.update(is_published=False)
    unpublish_reviews.short_description = "Unpublish selected reviews"

@admin.register(ReviewImage)
class ReviewImageAdmin(admin.ModelAdmin):
    list_display = ('review', 'caption', 'order')
    list_filter = ('review__created_at',)
    search_fields = ('review__title', 'caption')
    ordering = ('review', 'order')

@admin.register(ReviewLike)
class ReviewLikeAdmin(admin.ModelAdmin):
    list_display = ('review', 'user', 'created_at')
    list_filter = ('created_at',)
    search_fields = ('review__title', 'user__username')
    readonly_fields = ('created_at',)

@admin.register(ReviewReport)
class ReviewReportAdmin(admin.ModelAdmin):
    list_display = ('review', 'user', 'created_at', 'resolved', 'resolved_by')
    list_filter = ('resolved', 'created_at')
    search_fields = ('review__title', 'user__username', 'reason')
    readonly_fields = ('created_at', 'resolved_at')
    actions = ['mark_resolved', 'mark_unresolved']

    fieldsets = (
        ('Report Details', {
            'fields': ('review', 'user', 'reason')
        }),
        ('Resolution', {
            'fields': ('resolved', 'resolved_by', 'resolution_notes', 'resolved_at')
        }),
        ('Metadata', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        }),
    )

    def mark_resolved(self, request, queryset):
        queryset.update(resolved=True, resolved_by=request.user)
    mark_resolved.short_description = "Mark selected reports as resolved"

    def mark_unresolved(self, request, queryset):
        queryset.update(resolved=False, resolved_by=None, resolution_notes='')
    mark_unresolved.short_description = "Mark selected reports as unresolved"
