"""
Notifications admin
"""
from django.contrib import admin
from django.utils.html import format_html

from .models import NotificationLog, NotificationTemplate


@admin.register(NotificationLog)
class NotificationLogAdmin(admin.ModelAdmin):
    """Notification log admin"""
    list_display = [
        'channel', 'recipient', 'subject_preview',
        'status_badge', 'provider', 'retry_count',
        'sent_at', 'created_at'
    ]
    list_filter = ['channel', 'status', 'provider', 'created_at', 'sent_at']
    search_fields = ['recipient', 'subject', 'message', 'provider_message_id']
    ordering = ['-created_at']
    date_hierarchy = 'created_at'
    
    fieldsets = (
        ('Delivery', {
            'fields': ('channel', 'recipient', 'subject', 'message')
        }),
        ('Status', {
            'fields': ('status', 'sent_at', 'delivered_at', 'error', 'retry_count')
        }),
        ('Provider', {
            'fields': ('provider', 'provider_message_id', 'provider_response'),
            'classes': ('collapse',)
        }),
        ('Related Object', {
            'fields': ('content_type', 'object_id'),
            'classes': ('collapse',)
        }),
        ('Metadata', {
            'fields': ('metadata', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = [
        'sent_at', 'delivered_at', 'provider_message_id',
        'provider_response', 'created_at', 'updated_at'
    ]
    
    def has_add_permission(self, request):
        # Notifications should only be created programmatically
        return False
    
    def has_delete_permission(self, request, obj=None):
        # Keep notification logs for compliance
        return False
    
    def subject_preview(self, obj):
        """Show preview of subject"""
        if obj.subject:
            return obj.subject[:50] + '...' if len(obj.subject) > 50 else obj.subject
        return '-'
    subject_preview.short_description = 'Subject'
    
    def status_badge(self, obj):
        """Display status with color badge"""
        colors = {
            'pending': 'warning',
            'sending': 'info',
            'sent': 'success',
            'delivered': 'success',
            'failed': 'danger',
            'bounced': 'danger',
        }
        color = colors.get(obj.status, 'secondary')
        return format_html(
            '<span class="badge bg-{}">{}</span>',
            color,
            obj.get_status_display()
        )
    status_badge.short_description = 'Status'


@admin.register(NotificationTemplate)
class NotificationTemplateAdmin(admin.ModelAdmin):
    """Notification template admin"""
    list_display = ['name', 'template_type', 'is_active', 'created_at']
    list_filter = ['template_type', 'is_active']
    search_fields = ['name', 'description']
    ordering = ['name']
    
    fieldsets = (
        (None, {
            'fields': ('name', 'template_type', 'description')
        }),
        ('SMS Templates', {
            'fields': ('sms_template', 'sms_template_ar'),
            'classes': ('collapse',)
        }),
        ('WhatsApp Templates', {
            'fields': ('whatsapp_template', 'whatsapp_template_ar'),
            'classes': ('collapse',)
        }),
        ('Email Templates', {
            'fields': (
                'email_subject', 'email_subject_ar',
                'email_template', 'email_template_ar'
            ),
            'classes': ('collapse',)
        }),
        ('Status', {
            'fields': ('is_active',)
        }),
        ('Metadata', {
            'fields': ('created_at', 'updated_at')
        }),
    )
    
    readonly_fields = ['created_at', 'updated_at']
