"""
Template tags for displaying sentiment analysis results
"""
from django import template
from django.contrib.contenttypes.models import ContentType

from apps.ai_engine.models import SentimentResult
from apps.ai_engine.utils import (
    get_sentiment_badge_class,
    get_sentiment_icon,
    format_sentiment_score,
    format_confidence,
)

register = template.Library()


@register.simple_tag
def get_sentiment(obj):
    """
    Get sentiment result for an object.
    
    Usage: {% get_sentiment complaint as sentiment %}
    """
    try:
        content_type = ContentType.objects.get_for_model(obj)
        return SentimentResult.objects.filter(
            content_type=content_type,
            object_id=obj.id
        ).first()
    except Exception:
        return None


@register.inclusion_tag('ai_engine/tags/sentiment_badge.html')
def sentiment_badge(obj, size='sm'):
    """
    Display sentiment badge for an object.
    
    Usage: {% sentiment_badge complaint %}
    Usage: {% sentiment_badge complaint size='lg' %}
    """
    try:
        content_type = ContentType.objects.get_for_model(obj)
        sentiment = SentimentResult.objects.filter(
            content_type=content_type,
            object_id=obj.id
        ).first()
        
        return {
            'sentiment': sentiment,
            'size': size,
            'badge_class': get_sentiment_badge_class(sentiment.sentiment) if sentiment else '',
            'icon': get_sentiment_icon(sentiment.sentiment) if sentiment else '',
        }
    except Exception:
        return {'sentiment': None, 'size': size}


@register.inclusion_tag('ai_engine/tags/sentiment_card.html')
def sentiment_card(obj):
    """
    Display detailed sentiment card for an object.
    
    Usage: {% sentiment_card complaint %}
    """
    try:
        content_type = ContentType.objects.get_for_model(obj)
        sentiment = SentimentResult.objects.filter(
            content_type=content_type,
            object_id=obj.id
        ).first()
        
        return {
            'sentiment': sentiment,
            'badge_class': get_sentiment_badge_class(sentiment.sentiment) if sentiment else '',
            'icon': get_sentiment_icon(sentiment.sentiment) if sentiment else '',
            'score_formatted': format_sentiment_score(float(sentiment.sentiment_score)) if sentiment else '',
            'confidence_formatted': format_confidence(float(sentiment.confidence)) if sentiment else '',
        }
    except Exception:
        return {'sentiment': None}


@register.filter
def sentiment_badge_class(sentiment_value):
    """
    Get badge class for sentiment value.
    
    Usage: {{ sentiment|sentiment_badge_class }}
    """
    return get_sentiment_badge_class(sentiment_value)


@register.filter
def sentiment_icon(sentiment_value):
    """
    Get icon for sentiment value.
    
    Usage: {{ sentiment|sentiment_icon }}
    """
    return get_sentiment_icon(sentiment_value)


@register.filter
def format_score(score):
    """
    Format sentiment score.
    
    Usage: {{ score|format_score }}
    """
    try:
        return format_sentiment_score(float(score))
    except (ValueError, TypeError):
        return score


@register.filter
def format_conf(confidence):
    """
    Format confidence as percentage.
    
    Usage: {{ confidence|format_conf }}
    """
    try:
        return format_confidence(float(confidence))
    except (ValueError, TypeError):
        return confidence


@register.simple_tag
def has_sentiment(obj):
    """
    Check if object has sentiment analysis.
    
    Usage: {% has_sentiment complaint as has_sent %}
    """
    try:
        content_type = ContentType.objects.get_for_model(obj)
        return SentimentResult.objects.filter(
            content_type=content_type,
            object_id=obj.id
        ).exists()
    except Exception:
        return False
