"""Models for app comment."""
from django.contrib.contenttypes import fields as generic
from django.db import models
from django.utils.translation import gettext_lazy as _

from account.models import User
from translation.models import Language
from utils.models import ProjectBaseMixin
from utils.querysets import ContentTypeQuerySetMixin


class CommentQuerySet(ContentTypeQuerySetMixin):
    """QuerySets for Comment model."""

    def by_user(self, user: User):
        """Return comments by author"""
        return self.filter(user=user)

    def annotate_is_mine_status(self, user):
        """Annotate belonging status"""
        return self.annotate(is_mine=models.Case(
            models.When(
                models.Q(user=user if user.is_authenticated else None),
                then=True
            ),
            default=False,
            output_field=models.BooleanField()
        ))


class Comment(ProjectBaseMixin):
    """Comment model."""
    text = models.TextField(verbose_name=_('Comment text'))
    mark = models.PositiveIntegerField(blank=True, null=True, default=None, verbose_name=_('Mark'))
    user = models.ForeignKey('account.User', related_name='comments', on_delete=models.CASCADE, verbose_name=_('User'))
    old_id = models.IntegerField(null=True, blank=True, default=None)
    is_publish = models.BooleanField(default=False, verbose_name=_('Publish status'))
    site = models.ForeignKey('main.SiteSettings', blank=True, null=True,
                                on_delete=models.SET_NULL, verbose_name=_('site'))
    content_type = models.ForeignKey(generic.ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    objects = CommentQuerySet.as_manager()

    class Meta:
        """Meta class"""
        verbose_name = _('Comment')
        verbose_name_plural = _('Comments')

    def __str__(self):
        """String representation"""
        return str(self.user)
