from django.db import models
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation
from django.contrib.contenttypes.models import ContentType


class Comment(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="comments",
        verbose_name="کاربر"
    )

    text = models.TextField(verbose_name="متن نظر")

    create_date = models.DateTimeField(auto_now_add=True,verbose_name='تاریخ ایجاد')

    is_active = models.BooleanField(default=False,verbose_name='فعال/غیرفعال')

    # ✅ بخش مهم (Generic Relation)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    parent = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        on_delete=models.CASCADE,
        related_name="replies",
        verbose_name='والد'
    )
    # ⭐ امتیاز
    rating = models.PositiveSmallIntegerField(
        null=True,
        blank=True
    )
    comments = GenericRelation('core.Comment')

    class Meta:
        ordering = ['-create_date']
        verbose_name = 'نظر'
        verbose_name_plural = "نظرات "

    def __str__(self):
        return f"{self.user} - {self.text[:30]}"
