from django.contrib import admin

from apps.standards.models import (
    StandardSource,
    StandardCategory,
    Standard,
    StandardCompliance,
    StandardAttachment,
    ActivityType,
)


@admin.register(StandardSource)
class StandardSourceAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'is_active', 'created_at']
    list_filter = ['is_active', 'created_at']
    search_fields = ['name', 'name_ar', 'code', 'description']
    ordering = ['name']


@admin.register(StandardCategory)
class StandardCategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'source', 'order', 'max_score', 'is_active', 'created_at']
    list_filter = ['source', 'is_active', 'created_at']
    search_fields = ['name', 'name_ar', 'description']
    ordering = ['source', 'order', 'name']


@admin.register(ActivityType)
class ActivityTypeAdmin(admin.ModelAdmin):
    list_display = ['name', 'is_active', 'created_at']
    list_filter = ['is_active', 'created_at']
    search_fields = ['name', 'name_ar', 'description']
    ordering = ['name']


@admin.register(Standard)
class StandardAdmin(admin.ModelAdmin):
    list_display = ['code', 'title', 'source', 'category', 'activity_type', 'is_heading', 'is_assessable', 'is_active', 'effective_date']
    list_filter = ['source', 'category', 'departments', 'activity_type', 'is_heading', 'is_assessable', 'is_active', 'effective_date']
    search_fields = ['code', 'title', 'title_ar', 'description']
    ordering = ['source', 'category', 'order_within_category', 'code']
    date_hierarchy = 'effective_date'
    raw_id_fields = ['parent_standard']


@admin.register(StandardCompliance)
class StandardComplianceAdmin(admin.ModelAdmin):
    list_display = ['hospital', 'standard', 'status', 'priority', 'score', 'max_score', 'last_assessed_date', 'created_at']
    list_filter = ['status', 'priority', 'hospital', 'last_assessed_date', 'created_at']
    search_fields = ['hospital__name_en', 'standard__code', 'standard__title', 'notes', 'recommendations', 'corrective_action']
    ordering = ['-created_at']
    raw_id_fields = ['hospital', 'department', 'standard', 'assessor']


@admin.register(StandardAttachment)
class StandardAttachmentAdmin(admin.ModelAdmin):
    list_display = ['filename', 'compliance', 'standard', 'uploaded_by', 'created_at']
    list_filter = ['created_at']
    search_fields = ['filename', 'description']
    ordering = ['-created_at']
    raw_id_fields = ['compliance', 'standard', 'uploaded_by']
