"""
AI Engine admin
"""
from django.contrib import admin
from django.utils.html import format_html

from .models import SentimentResult


@admin.register(SentimentResult)
class SentimentResultAdmin(admin.ModelAdmin):
    """Sentiment result admin"""
    list_display = [
        'text_preview', 'sentiment_badge', 'sentiment_score',
        'confidence', 'ai_service', 'language', 'created_at'
    ]
    list_filter = ['sentiment', 'ai_service', 'language', 'created_at']
    search_fields = ['text']
    ordering = ['-created_at']
    date_hierarchy = 'created_at'
    
    fieldsets = (
        ('Related Object', {
            'fields': ('content_type', 'object_id')
        }),
        ('Text', {
            'fields': ('text', 'language')
        }),
        ('Sentiment Analysis', {
            'fields': ('sentiment', 'sentiment_score', 'confidence')
        }),
        ('AI Service', {
            'fields': ('ai_service', 'ai_model', 'processing_time_ms')
        }),
        ('Additional Analysis', {
            'fields': ('keywords', 'entities', 'emotions'),
            'classes': ('collapse',)
        }),
        ('Metadata', {
            'fields': ('metadata', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ['created_at', 'updated_at']
    
    def has_add_permission(self, request):
        # Sentiment results should only be created programmatically
        return False
    
    def text_preview(self, obj):
        """Show preview of text"""
        return obj.text[:100] + '...' if len(obj.text) > 100 else obj.text
    text_preview.short_description = 'Text'
    
    def sentiment_badge(self, obj):
        """Display sentiment with badge"""
        colors = {
            'positive': 'success',
            'neutral': 'secondary',
            'negative': 'danger',
        }
        color = colors.get(obj.sentiment, 'secondary')
        
        return format_html(
            '<span class="badge bg-{}">{}</span>',
            color,
            obj.get_sentiment_display()
        )
    sentiment_badge.short_description = 'Sentiment'
