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


class Organisation(models.Model):
    name = models.CharField(
        verbose_name=_("Name"),
        help_text=_("Organisation name"),
        max_length=512,
        null=False,
        unique=True,
    )

    website = models.URLField(
        verbose_name=_("Website"),
        help_text=_("Website of the organisation"),
        max_length=1024,
        blank=True,
        null=True,
    )

    description = CKEditor5Field(
        verbose_name=_("Description"),
        help_text=_("Description of the organisation."),
        blank=True,
        null=True,
    )

    principal_place = models.ForeignKey(
        "Place",
        verbose_name=_("Principal place"),
        help_text=_(
            "Place mainly associated with this organizer. Mainly used if there is a similarity in the name, to avoid redundant displays."
        ),
        null=True,
        on_delete=models.SET_NULL,
        blank=True,
    )

    class Meta:
        verbose_name = _("Organisation")
        verbose_name_plural = _("Organisations")

    def __str__(self):
        return self.name

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