"""
AI Engine serializers
"""
from rest_framework import serializers

from .models import SentimentResult


class SentimentResultSerializer(serializers.ModelSerializer):
    """Sentiment result serializer"""
    
    content_type_name = serializers.SerializerMethodField()
    
    class Meta:
        model = SentimentResult
        fields = [
            'id',
            'content_type',
            'content_type_name',
            'object_id',
            'text',
            'language',
            'sentiment',
            'sentiment_score',
            'confidence',
            'ai_service',
            'ai_model',
            'processing_time_ms',
            'keywords',
            'entities',
            'emotions',
            'metadata',
            'created_at',
            'updated_at',
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']
    
    def get_content_type_name(self, obj):
        """Get human-readable content type name"""
        return obj.content_type.model if obj.content_type else None


class AnalyzeTextRequestSerializer(serializers.Serializer):
    """Request serializer for text analysis"""
    
    text = serializers.CharField(
        required=True,
        help_text="Text to analyze"
    )
    language = serializers.ChoiceField(
        choices=['en', 'ar'],
        required=False,
        allow_null=True,
        help_text="Language code (auto-detected if not provided)"
    )
    extract_keywords = serializers.BooleanField(
        default=True,
        help_text="Whether to extract keywords"
    )
    extract_entities = serializers.BooleanField(
        default=True,
        help_text="Whether to extract entities"
    )
    detect_emotions = serializers.BooleanField(
        default=True,
        help_text="Whether to detect emotions"
    )


class AnalyzeTextResponseSerializer(serializers.Serializer):
    """Response serializer for text analysis"""
    
    text = serializers.CharField()
    language = serializers.CharField()
    sentiment = serializers.CharField()
    sentiment_score = serializers.FloatField()
    confidence = serializers.FloatField()
    keywords = serializers.ListField(child=serializers.CharField())
    entities = serializers.ListField(child=serializers.DictField())
    emotions = serializers.DictField()
    ai_service = serializers.CharField()
    ai_model = serializers.CharField()
    processing_time_ms = serializers.IntegerField()


class BatchAnalyzeRequestSerializer(serializers.Serializer):
    """Request serializer for batch text analysis"""
    
    texts = serializers.ListField(
        child=serializers.CharField(),
        required=True,
        help_text="List of texts to analyze"
    )
    language = serializers.ChoiceField(
        choices=['en', 'ar'],
        required=False,
        allow_null=True,
        help_text="Language code (auto-detected if not provided)"
    )


class BatchAnalyzeResponseSerializer(serializers.Serializer):
    """Response serializer for batch text analysis"""
    
    results = AnalyzeTextResponseSerializer(many=True)
    total = serializers.IntegerField()
    processing_time_ms = serializers.IntegerField()


class SentimentStatsSerializer(serializers.Serializer):
    """Sentiment statistics serializer"""
    
    total = serializers.IntegerField()
    positive = serializers.IntegerField()
    neutral = serializers.IntegerField()
    negative = serializers.IntegerField()
    positive_pct = serializers.FloatField()
    neutral_pct = serializers.FloatField()
    negative_pct = serializers.FloatField()
    avg_score = serializers.FloatField()
    avg_confidence = serializers.FloatField()
