from django.db import models
from django.utils import timezone


class SocialPlatform(models.TextChoices):
    """Social media platform choices"""
    FACEBOOK = 'facebook', 'Facebook'
    INSTAGRAM = 'instagram', 'Instagram'
    YOUTUBE = 'youtube', 'YouTube'
    TWITTER = 'twitter', 'Twitter/X'
    LINKEDIN = 'linkedin', 'LinkedIn'
    TIKTOK = 'tiktok', 'TikTok'
    GOOGLE = 'google', 'Google Reviews'


class SocialMediaComment(models.Model):
    """
    Model to store social media comments from various platforms with AI analysis.
    Stores scraped comments and AI-powered sentiment, keywords, topics, and entity analysis.
    """
    
    # --- Core ---
    id = models.BigAutoField(primary_key=True)
    platform = models.CharField(
        max_length=50, 
        choices=SocialPlatform.choices,
        db_index=True,
        help_text="Social media platform"
    )
    comment_id = models.CharField(
        max_length=255, 
        db_index=True,
        help_text="Unique comment ID from the platform"
    )
    
    # --- Content ---
    comments = models.TextField(help_text="Comment text content")
    author = models.CharField(max_length=255, null=True, blank=True, help_text="Comment author")
    
    # --- Raw Data ---
    raw_data = models.JSONField(
        default=dict,
        help_text="Complete raw data from platform API"
    )
    
    # --- Metadata ---
    post_id = models.CharField(
        max_length=255, 
        null=True, 
        blank=True,
        help_text="ID of the post/media"
    )
    media_url = models.URLField(
        max_length=500, 
        null=True, 
        blank=True,
        help_text="URL to associated media"
    )
    
    # --- Engagement ---
    like_count = models.IntegerField(default=0, help_text="Number of likes")
    reply_count = models.IntegerField(default=0, help_text="Number of replies")
    rating = models.IntegerField(
        null=True,
        blank=True,
        db_index=True,
        help_text="Star rating (1-5) for review platforms like Google Reviews"
    )
    
    # --- Timestamps ---
    published_at = models.DateTimeField(
        null=True, 
        blank=True,
        db_index=True,
        help_text="When the comment was published"
    )
    scraped_at = models.DateTimeField(
        auto_now_add=True,
        db_index=True,
        help_text="When the comment was scraped"
    )
    
    # --- AI Bilingual Analysis ---
    ai_analysis = models.JSONField(
        default=dict,
        blank=True,
        db_index=True,
        help_text="Complete AI analysis in bilingual format (en/ar) with sentiment, summaries, keywords, topics, entities, and emotions"
    )
    
    class Meta:
        ordering = ['-published_at']
        unique_together = ['platform', 'comment_id']
        indexes = [
            models.Index(fields=['platform']),
            models.Index(fields=['published_at']),
            models.Index(fields=['platform', '-published_at']),
            models.Index(fields=['ai_analysis'], name='idx_ai_analysis'),
        ]
    
    def __str__(self):
        return f"{self.platform} - {self.author or 'Anonymous'}"
    
    @property
    def is_analyzed(self):
        """Check if comment has been AI analyzed"""
        return bool(self.ai_analysis)
