"""
Executive Summary Admin - Django admin interface for executive summary models
"""
from django.contrib import admin
from django.utils.html import format_html
from django.urls import reverse
from django.utils import timezone

from .models import ExecutiveMetric, ExecutiveReport, PredictiveInsight, AIRecommendation


@admin.register(ExecutiveMetric)
class ExecutiveMetricAdmin(admin.ModelAdmin):
    list_display = [
        'metric_type', 'metric_value', 'variance_display', 
        'metric_date', 'hospital', 'created_at'
    ]
    list_filter = ['metric_type', 'metric_date', 'hospital']
    date_hierarchy = 'metric_date'
    search_fields = ['metric_type', 'hospital__name']
    ordering = ['-metric_date']
    
    def variance_display(self, obj):
        if obj.variance is None:
            return "-"
        arrow = "↑" if obj.variance_direction == "up" else "↓" if obj.variance_direction == "down" else "→"
        color = "#10b981" if obj.variance_direction == "up" else "#ef4444" if obj.variance_direction == "down" else "#6b7280"
        return format_html(
            '<span style="color: {}; font-weight: bold;">{} {}%</span>',
            color, arrow, obj.variance
        )
    variance_display.short_description = "Variance"


@admin.register(ExecutiveReport)
class ExecutiveReportAdmin(admin.ModelAdmin):
    list_display = [
        'report_type', 'status', 'start_date', 'end_date', 
        'generation_time_display', 'created_at', 'pdf_link'
    ]
    list_filter = ['report_type', 'status', 'created_at']
    date_hierarchy = 'created_at'
    readonly_fields = ['created_at', 'updated_at']
    ordering = ['-created_at']
    
    def generation_time_display(self, obj):
        if obj.generation_time_ms:
            return f"{obj.generation_time_ms / 1000:.2f}s"
        return "-"
    generation_time_display.short_description = "Gen Time"
    
    def pdf_link(self, obj):
        if obj.pdf_file:
            return format_html('<a href="{}" target="_blank">Download PDF</a>', obj.pdf_file.url)
        return "-"
    pdf_link.short_description = "PDF"


@admin.register(PredictiveInsight)
class PredictiveInsightAdmin(admin.ModelAdmin):
    list_display = [
        'title_en', 'severity_badge', 'insight_type', 
        'status', 'hospital', 'created_at', 'acknowledged_by_display'
    ]
    list_filter = ['severity', 'status', 'insight_type', 'hospital', 'created_at']
    search_fields = ['title_en', 'description_en', 'hospital__name']
    date_hierarchy = 'created_at'
    ordering = ['-severity', '-created_at']
    readonly_fields = ['created_at', 'updated_at', 'acknowledged_at']
    
    def severity_badge(self, obj):
        colors = {
            'low': '#3b82f6',
            'medium': '#f59e0b',
            'high': '#f97316',
            'critical': '#ef4444',
        }
        color = colors.get(obj.severity, '#6b7280')
        return format_html(
            '<span style="background-color: {}; color: white; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: bold;">{}</span>',
            color, obj.get_severity_display()
        )
    severity_badge.short_description = "Severity"
    
    def acknowledged_by_display(self, obj):
        if obj.acknowledged_by:
            return obj.acknowledged_by.get_full_name() or obj.acknowledged_by.email
        return "-"
    acknowledged_by_display.short_description = "Acknowledged By"


@admin.register(AIRecommendation)
class AIRecommendationAdmin(admin.ModelAdmin):
    list_display = [
        'title_en', 'category', 'priority_badge', 
        'status', 'hospital', 'created_at'
    ]
    list_filter = ['category', 'priority', 'status', 'hospital', 'created_at']
    search_fields = ['title_en', 'description_en', 'hospital__name']
    date_hierarchy = 'created_at'
    ordering = ['-priority', '-created_at']
    readonly_fields = ['created_at', 'updated_at']
    
    def priority_badge(self, obj):
        colors = {
            'low': '#3b82f6',
            'medium': '#f59e0b',
            'high': '#f97316',
            'urgent': '#ef4444',
        }
        color = colors.get(obj.priority, '#6b7280')
        return format_html(
            '<span style="background-color: {}; color: white; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: bold;">{}</span>',
            color, obj.get_priority_display()
        )
    priority_badge.short_description = "Priority"
