from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django_ckeditor_5.fields import CKEditor5Field


class Message(models.Model):
    class TYPE(models.TextChoices):
        FROM_CONTRIBUTOR = "from_contributor", _("From contributor")
        IMPORT_PROCESS = "import_process", _("Import process")
        UPDATE_PROCESS = "update_process", _("Update process")
        CONTACT_FORM = "contact_form", _("Contact form")
        EVENT_REPORT = "event_report", _("Event report")
        FROM_CONTRIBUTOR_NO_MSG = (
            "from_contrib_no_msg",
            _("From contributor (without message)"),
        )
        WARNING = "warning", _("Warning")

    class Meta:
        verbose_name = _("Message")
        verbose_name_plural = _("Messages")
        indexes = [
            models.Index(fields=["related_event"]),
            models.Index(fields=["user"]),
            models.Index(fields=["date"]),
            models.Index(fields=["spam", "closed"]),
        ]

    subject = models.CharField(
        verbose_name=_("Subject"),
        help_text=_("The subject of your message"),
        max_length=512,
    )

    related_event = models.ForeignKey(
        "Event",
        verbose_name=_("Related event"),
        help_text=_("The message is associated with this event."),
        null=True,
        default=None,
        on_delete=models.SET_DEFAULT,
    )

    user = models.ForeignKey(
        User,
        verbose_name=_("Author of the message"),
        null=True,
        default=None,
        on_delete=models.SET_DEFAULT,
    )

    name = models.CharField(
        verbose_name=_("Name"),
        help_text=_("Your name"),
        max_length=512,
        blank=True,
        null=True,
    )
    email = models.EmailField(
        verbose_name=_("Email address"),
        help_text=_("Your email address"),
        max_length=254,
        blank=True,
        null=True,
    )
    message = CKEditor5Field(
        verbose_name=_("Message"), help_text=_("Your message"), blank=True
    )

    date = models.DateTimeField(auto_now_add=True)

    spam = models.BooleanField(
        verbose_name=_("Spam"),
        help_text=_("This message is a spam."),
        default=False,
    )

    closed = models.BooleanField(
        verbose_name=_("Closed"),
        help_text=_(
            "this message has been processed and no longer needs to be handled"
        ),
        default=False,
    )
    comments = CKEditor5Field(
        verbose_name=_("Comments"),
        help_text=_("Comments on the message from the moderation team"),
        default="",
        blank=True,
        null=True,
    )

    message_type = models.CharField(
        verbose_name=_("Type"),
        max_length=20,
        choices=TYPE.choices,
        default=None,
        null=True,
    )

    @classmethod
    def nb_open_messages(cls):
        return Message.objects.filter(
            Q(closed=False)
            & Q(spam=False)
            & Q(
                message_type__in=[
                    Message.TYPE.CONTACT_FORM,
                    Message.TYPE.EVENT_REPORT,
                    Message.TYPE.FROM_CONTRIBUTOR,
                ]
            )
        ).count()

    def get_absolute_url(self):
        return reverse("message", kwargs={"pk": self.pk})
