"""
Physicians admin
"""
from django.contrib import admin

from .models import PhysicianMonthlyRating


@admin.register(PhysicianMonthlyRating)
class PhysicianMonthlyRatingAdmin(admin.ModelAdmin):
    """Physician monthly rating admin"""
    list_display = [
        'staff', 'year', 'month', 'average_rating',
        'total_surveys', 'hospital_rank', 'department_rank'
    ]
    list_filter = ['year', 'month', 'staff__hospital', 'staff__department']
    search_fields = [
        'staff__first_name', 'staff__last_name', 'staff__license_number'
    ]
    ordering = ['-year', '-month', '-average_rating']

    fieldsets = (
        ('Physician & Period', {
            'fields': ('staff', 'year', 'month')
        }),
        ('Ratings', {
            'fields': (
                'average_rating', 'total_surveys',
                'positive_count', 'neutral_count', 'negative_count'
            )
        }),
        ('Breakdown', {
            'fields': ('md_consult_rating',)
        }),
        ('Ranking', {
            'fields': ('hospital_rank', 'department_rank')
        }),
        ('Metadata', {
            'fields': ('metadata', '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('staff', 'staff__hospital', 'staff__department')
