"""
PX Sources admin configuration
"""
from django.contrib import admin
from django.utils.html import format_html, mark_safe

from .models import PXSource, SourceUsage, SourceUser


@admin.register(PXSource)
class PXSourceAdmin(admin.ModelAdmin):
    """PX Source admin interface"""
    list_display = [
        'name_en', 'name_ar',
        'is_active_badge', 'created_at'
    ]
    list_filter = [
        'is_active', 'created_at'
    ]
    search_fields = [
        'name_en', 'name_ar', 'description'
    ]
    ordering = ['name_en']
    date_hierarchy = 'created_at'
    
    fieldsets = (
        ('Basic Information', {
            'fields': ('name_en', 'name_ar')
        }),
        ('Description', {
            'fields': ('description',)
        }),
        ('Status', {
            'fields': ('is_active',)
        }),
        ('Metadata', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ['created_at', 'updated_at']
    
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.prefetch_related('usage_records')
    
    def is_active_badge(self, obj):
        """Display active status with badge"""
        if obj.is_active:
            return mark_safe('<span class="badge bg-success">Active</span>')
        return mark_safe('<span class="badge bg-secondary">Inactive</span>')
    is_active_badge.short_description = 'Status'
    is_active_badge.admin_order_field = 'is_active'


@admin.register(SourceUser)
class SourceUserAdmin(admin.ModelAdmin):
    """Source User admin interface"""
    list_display = [
        'user_email', 'source_name',
        'is_active_badge', 'created_at'
    ]
    list_filter = [
        'source', 'is_active', 'created_at'
    ]
    search_fields = [
        'user__email', 'user__first_name', 'user__last_name',
        'source__name_en', 'source__name_ar'
    ]
    ordering = ['source__name_en', 'user__email']
    date_hierarchy = 'created_at'
    
    fieldsets = (
        ('User & Source', {
            'fields': ('user', 'source')
        }),
        ('Status', {
            'fields': ('is_active',)
        }),
        ('Permissions', {
            'fields': ('can_create_complaints', 'can_create_inquiries')
        }),
        ('Metadata', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ['created_at', 'updated_at']
    
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related('user', 'source')
    
    def user_email(self, obj):
        """Display user email"""
        return obj.user.email
    user_email.short_description = 'User Email'
    user_email.admin_order_field = 'user__email'
    
    def source_name(self, obj):
        """Display source name"""
        return obj.source.name_en
    source_name.short_description = 'Source'
    source_name.admin_order_field = 'source__name_en'
    
    def is_active_badge(self, obj):
        """Display active status with badge"""
        if obj.is_active:
            return mark_safe('<span class="badge bg-success">Active</span>')
        return mark_safe('<span class="badge bg-secondary">Inactive</span>')
    is_active_badge.short_description = 'Status'
    is_active_badge.admin_order_field = 'is_active'


@admin.register(SourceUsage)
class SourceUsageAdmin(admin.ModelAdmin):
    """Source Usage admin interface"""
    list_display = [
        'source', 'content_type', 'object_id',
        'hospital', 'user', 'created_at'
    ]
    list_filter = [
        'source', 'content_type', 'hospital', 'created_at'
    ]
    search_fields = [
        'source__name_en', 'object_id', 'user__email'
    ]
    ordering = ['-created_at']
    date_hierarchy = 'created_at'
    
    fieldsets = (
        (None, {
            'fields': ('source', 'content_type', 'object_id')
        }),
        ('Context', {
            'fields': ('hospital', 'user')
        }),
        ('Metadata', {
            'fields': ('created_at', 'updated_at')
        }),
    )
    
    readonly_fields = ['created_at', 'updated_at']
    
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related('source', 'hospital', 'user', 'content_type')
