from django.db import models
import uuid
from mptt.models import MPTTModel, TreeForeignKey


class Tenants(MPTTModel):
    tenant_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    parent_tenant = TreeForeignKey(
        "self",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name="children",
    )
    name = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    level = models.PositiveIntegerField(default=0, editable=False)
    lft = models.PositiveIntegerField(default=1, editable=False)
    rght = models.PositiveIntegerField(default=2, editable=False)
    tree_id = models.PositiveIntegerField(default=1, editable=False)

    class MPTTMeta:
        order_insertion_by = ["name"]
        # Add the following line to tell MPTT the name of the parent field
        parent_attr = "parent_tenant"

    class Meta:
        verbose_name = "Tenant"
        verbose_name_plural = "Tenants"
        ordering = ["name"]

    def __str__(self):
        return self.name
