"""
Organizations admin
"""
from django.contrib import admin

from .models import Department, Hospital, Organization, Patient, Staff


@admin.register(Organization)
class OrganizationAdmin(admin.ModelAdmin):
    """Organization admin"""
    list_display = ['name', 'code', 'city', 'status', 'created_at']
    list_filter = ['status', 'city']
    search_fields = ['name', 'name_ar', 'code', 'license_number']
    ordering = ['name']

    fieldsets = (
        (None, {'fields': ('name', 'name_ar', 'code')}),
        ('Contact Information', {'fields': ('address', 'city', 'phone', 'email', 'website')}),
        ('Details', {'fields': ('license_number', 'status', 'logo')}),
        ('Metadata', {'fields': ('created_at', 'updated_at')}),
    )

    readonly_fields = ['created_at', 'updated_at']


@admin.register(Hospital)
class HospitalAdmin(admin.ModelAdmin):
    """Hospital admin"""
    list_display = ['name', 'code', 'city', 'status', 'capacity', 'created_at']
    list_filter = ['status', 'city']
    search_fields = ['name', 'name_ar', 'code', 'license_number']
    ordering = ['name']

    fieldsets = (
        (None, {'fields': ('organization', 'name', 'name_ar', 'code')}),
        ('Contact Information', {'fields': ('address', 'city', 'phone', 'email')}),
        ('Details', {'fields': ('license_number', 'capacity', 'status')}),
        ('Metadata', {'fields': ('created_at', 'updated_at')}),
    )
    autocomplete_fields = ['organization']

    readonly_fields = ['created_at', 'updated_at']


@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    """Department admin"""
    list_display = ['name', 'hospital', 'code', 'manager', 'status', 'created_at']
    list_filter = ['status', 'hospital']
    search_fields = ['name', 'name_ar', 'code']
    ordering = ['hospital', 'name']
    autocomplete_fields = ['hospital', 'parent', 'manager']

    fieldsets = (
        (None, {'fields': ('hospital', 'name', 'name_ar', 'code')}),
        ('Hierarchy', {'fields': ('parent', 'manager')}),
        ('Contact', {'fields': ('phone', 'email', 'location')}),
        ('Status', {'fields': ('status',)}),
        ('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('hospital', 'manager', 'parent')


@admin.register(Staff)
class StaffAdmin(admin.ModelAdmin):
    """Staff admin"""
    list_display = ['__str__', 'staff_type', 'job_title', 'employee_id', 'hospital', 'department', 'status']
    list_filter = ['status', 'hospital', 'staff_type', 'specialization']
    search_fields = ['first_name', 'last_name', 'first_name_ar', 'last_name_ar', 'employee_id', 'license_number', 'job_title']
    ordering = ['last_name', 'first_name']
    autocomplete_fields = ['hospital', 'department', 'user']

    fieldsets = (
        (None, {'fields': ('first_name', 'last_name', 'first_name_ar', 'last_name_ar')}),
        ('Role', {'fields': ('staff_type', 'job_title')}),
        ('Professional', {'fields': ('license_number', 'specialization', 'employee_id', 'email')}),
        ('Organization', {'fields': ('hospital', 'department')}),
        ('Account', {'fields': ('user',)}),
        ('Status', {'fields': ('status',)}),
        ('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('hospital', 'department', 'user')


@admin.register(Patient)
class PatientAdmin(admin.ModelAdmin):
    """Patient admin"""
    list_display = ['get_full_name', 'mrn', 'national_id', 'phone', 'primary_hospital', 'status']
    list_filter = ['status', 'gender', 'primary_hospital', 'city']
    search_fields = ['mrn', 'national_id', 'first_name', 'last_name', 'first_name_ar', 'last_name_ar', 'phone', 'email']
    ordering = ['last_name', 'first_name']
    autocomplete_fields = ['primary_hospital']

    fieldsets = (
        (None, {'fields': ('mrn', 'national_id')}),
        ('Personal Information', {'fields': ('first_name', 'last_name', 'first_name_ar', 'last_name_ar')}),
        ('Demographics', {'fields': ('date_of_birth', 'gender')}),
        ('Contact', {'fields': ('phone', 'email', 'address', 'city')}),
        ('Hospital', {'fields': ('primary_hospital',)}),
        ('Status', {'fields': ('status',)}),
        ('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('primary_hospital')
