# models.py
from django.db import models
# Note: JSONField is automatically used for PostgreSQL by Django 3.1+
# If using an older Django, you might need: from django.contrib.postgres.fields import JSONField

class Product(models.Model):
    """Product model to store basic product information"""
    sku = models.CharField(max_length=100, unique=True)
    category = models.CharField(max_length=100)
    title = models.TextField()
    description = models.TextField(blank=True)
    # New fields for completeness (optional, but good practice)
    short_description = models.TextField(blank=True)
    seo_title = models.CharField(max_length=255, blank=True)
    seo_description = models.TextField(blank=True)

    attributes = models.JSONField(default=dict)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    image_path = models.TextField(blank=True)
    
    class Meta:
        indexes = [
            models.Index(fields=['category']),
            models.Index(fields=['sku']),
        ]

    def __str__(self):
        return f"{self.sku} - {self.title}"

# ... AttributeScore model remains the same ...
class AttributeScore(models.Model):
    """Store attribute quality scores"""
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='attribute_scores')
    score = models.FloatField()
    max_score = models.FloatField(default=100.0)
    details = models.JSONField(default=dict)
    issues = models.JSONField(default=list)
    suggestions = models.JSONField(default=list)
    ai_suggestions = models.JSONField(default=dict, blank=True)  # Gemini AI suggestions
    processing_time = models.FloatField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        indexes = [
            models.Index(fields=['-created_at']),
        ]

    def __str__(self):
        return f"{self.product.sku} - Score: {self.score}/{self.max_score}"

# ... CategoryAttributeRule model remains the same ...
class CategoryAttributeRule(models.Model):
    """Define mandatory attributes per category"""
    category = models.CharField(max_length=100)
    attribute_name = models.CharField(max_length=100)
    is_mandatory = models.BooleanField(default=False)
    valid_values = models.JSONField(default=list, blank=True)
    data_type = models.CharField(max_length=50, default='string')
    validation_regex = models.CharField(max_length=500, blank=True)
    min_length = models.IntegerField(null=True, blank=True)
    max_length = models.IntegerField(null=True, blank=True)
    description = models.TextField(blank=True)

    class Meta:
        unique_together = ('category', 'attribute_name')
        indexes = [
            models.Index(fields=['category']),
        ]

    def __str__(self):
        return f"{self.category} - {self.attribute_name}"

# --- NEW MODEL FOR CONTENT RULES ---
class ProductContentRule(models.Model):
    """Define rules for general product content fields (title, description, SEO)"""
    category = models.CharField(max_length=100, blank=True, null=True, help_text="Category or NULL for a global rule.")
    field_name = models.CharField(max_length=100, help_text="e.g., 'title', 'description', 'seo_title'")
    is_mandatory = models.BooleanField(default=True)
    min_length = models.IntegerField(null=True, blank=True, help_text="Minimum character length.")
    max_length = models.IntegerField(null=True, blank=True, help_text="Maximum character length.")
    min_word_count = models.IntegerField(null=True, blank=True, help_text="Minimum word count.")
    max_word_count = models.IntegerField(null=True, blank=True, help_text="Maximum word count.")
    must_contain_keywords = models.JSONField(default=list, blank=True, help_text="List of keywords (case-insensitive) that must be present.")
    validation_regex = models.CharField(max_length=500, blank=True, help_text="A regex to validate the field content.")
    description = models.TextField(blank=True)

    class Meta:
        unique_together = ('category', 'field_name')
        indexes = [
            models.Index(fields=['field_name']),
            models.Index(fields=['category', 'field_name']),
        ]

    def __str__(self):
        category_str = self.category if self.category else 'GLOBAL'
        return f"{category_str} - Content Rule: {self.field_name}"