"""
Call Center admin
"""
from django.contrib import admin
from django.utils.html import format_html

from .models import CallCenterInteraction


@admin.register(CallCenterInteraction)
class CallCenterInteractionAdmin(admin.ModelAdmin):
    """Call center interaction admin"""
    list_display = [
        'caller_info', 'call_type', 'hospital', 'agent',
        'satisfaction_badge', 'wait_time_display', 'call_duration_display',
        'resolved', 'call_started_at'
    ]
    list_filter = [
        'call_type', 'is_low_rating', 'resolved',
        'hospital', 'department', 'call_started_at'
    ]
    search_fields = [
        'subject', 'notes', 'caller_name', 'caller_phone',
        'patient__mrn', 'patient__first_name', 'patient__last_name'
    ]
    ordering = ['-call_started_at']
    date_hierarchy = 'call_started_at'
    
    fieldsets = (
        ('Caller Information', {
            'fields': ('patient', 'caller_name', 'caller_phone', 'caller_relationship')
        }),
        ('Organization', {
            'fields': ('hospital', 'department', 'agent')
        }),
        ('Call Details', {
            'fields': ('call_type', 'subject', 'notes')
        }),
        ('Metrics', {
            'fields': ('wait_time_seconds', 'call_duration_seconds', 'satisfaction_rating', 'is_low_rating')
        }),
        ('Resolution', {
            'fields': ('resolved', 'resolution_notes')
        }),
        ('Timestamps', {
            'fields': ('call_started_at', 'call_ended_at', 'created_at', 'updated_at')
        }),
        ('Metadata', {
            'fields': ('metadata',),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ['is_low_rating', 'call_started_at', 'created_at', 'updated_at']
    
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related('patient', 'hospital', 'department', 'agent')
    
    def caller_info(self, obj):
        """Display caller information"""
        if obj.patient:
            return f"{obj.patient.get_full_name()} (MRN: {obj.patient.mrn})"
        return obj.caller_name or 'Unknown'
    caller_info.short_description = 'Caller'
    
    def satisfaction_badge(self, obj):
        """Display satisfaction rating with badge"""
        if obj.satisfaction_rating is None:
            return '-'
        
        if obj.satisfaction_rating >= 4:
            color = 'success'
        elif obj.satisfaction_rating >= 3:
            color = 'warning'
        else:
            color = 'danger'
        
        return format_html(
            '<span class="badge bg-{}">{}/5</span>',
            color,
            obj.satisfaction_rating
        )
    satisfaction_badge.short_description = 'Satisfaction'
    
    def wait_time_display(self, obj):
        """Display wait time in minutes"""
        if obj.wait_time_seconds:
            minutes = obj.wait_time_seconds // 60
            return f"{minutes} min"
        return '-'
    wait_time_display.short_description = 'Wait Time'
    
    def call_duration_display(self, obj):
        """Display call duration in minutes"""
        if obj.call_duration_seconds:
            minutes = obj.call_duration_seconds // 60
            return f"{minutes} min"
        return '-'
    call_duration_display.short_description = 'Duration'
