"""
References admin - Django admin configuration for Reference Section
"""
from django.contrib import admin
from django.utils.html import format_html

from apps.references.models import ReferenceFolder, ReferenceDocument, ReferenceDocumentAccess


@admin.register(ReferenceFolder)
class ReferenceFolderAdmin(admin.ModelAdmin):
    """Admin interface for ReferenceFolder"""
    list_display = ['name', 'name_ar', 'hospital', 'parent', 'get_document_count', 'is_active', 'order']
    list_filter = ['hospital', 'is_active', 'parent']
    search_fields = ['name', 'name_ar', 'description', 'description_ar']
    prepopulated_fields = {}
    readonly_fields = ['created_at', 'updated_at', 'get_full_path_display']

    fieldsets = (
        ('Basic Information', {
            'fields': ('hospital', 'name', 'name_ar', 'parent')
        }),
        ('Description', {
            'fields': ('description', 'description_ar')
        }),
        ('UI Customization', {
            'fields': ('icon', 'color', 'order')
        }),
        ('Access Control', {
            'fields': ('access_roles', 'is_active')
        }),
        ('System Information', {
            'fields': ('get_full_path_display', 'is_deleted', 'deleted_at', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    filter_horizontal = ['access_roles']

    def get_full_path_display(self, obj):
        """Display full path in admin"""
        return obj.get_full_path()
    get_full_path_display.short_description = 'Full Path'

    def get_document_count(self, obj):
        """Display document count"""
        count = obj.get_document_count()
        return f"{count} document{'s' if count != 1 else ''}"
    get_document_count.short_description = 'Documents'


@admin.register(ReferenceDocument)
class ReferenceDocumentAdmin(admin.ModelAdmin):
    """Admin interface for ReferenceDocument"""
    list_display = ['title', 'folder', 'hospital', 'get_file_icon_display', 'version', 'is_latest_version', 'download_count', 'is_published']
    list_filter = ['hospital', 'folder', 'file_type', 'is_latest_version', 'is_published']
    search_fields = ['title', 'title_ar', 'filename', 'description', 'description_ar', 'tags']
    prepopulated_fields = {}
    readonly_fields = ['file_type', 'file_size', 'download_count', 'created_at', 'updated_at']

    fieldsets = (
        ('Basic Information', {
            'fields': ('hospital', 'folder', 'title', 'title_ar')
        }),
        ('File', {
            'fields': ('file', 'filename', 'file_type', 'file_size', 'get_file_icon_display')
        }),
        ('Description', {
            'fields': ('description', 'description_ar')
        }),
        ('Versioning', {
            'fields': ('version', 'is_latest_version', 'parent_document')
        }),
        ('Upload Information', {
            'fields': ('uploaded_by',)
        }),
        ('Usage Tracking', {
            'fields': ('download_count', 'last_accessed_at')
        }),
        ('Access Control', {
            'fields': ('access_roles', 'is_published')
        }),
        ('Tags', {
            'fields': ('tags', 'metadata')
        }),
        ('System Information', {
            'fields': ('is_deleted', 'deleted_at', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    filter_horizontal = ['access_roles']

    def get_file_icon_display(self, obj):
        """Display file icon"""
        icon_class = obj.get_file_icon()
        return format_html('<i class="fa {}"></i>', icon_class)
    get_file_icon_display.short_description = 'File Type'


@admin.register(ReferenceDocumentAccess)
class ReferenceDocumentAccessAdmin(admin.ModelAdmin):
    """Admin interface for ReferenceDocumentAccess"""
    list_display = ['document', 'user', 'action', 'ip_address', 'created_at']
    list_filter = ['action', 'created_at', 'document__hospital']
    search_fields = ['user__email', 'user__username', 'document__title']
    readonly_fields = ['created_at']

    fieldsets = (
        ('Access Details', {
            'fields': ('document', 'user', 'action')
        }),
        ('Context', {
            'fields': ('ip_address', 'user_agent')
        }),
        ('System Information', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        }),
    )
