# (c) cavaliba.com - IAM - models.py

# DEPRECATED IN V4.0

from django.db import models
from django.utils.translation import gettext_lazy as _

# # YAML export
# class MyYamlDumper(yaml.SafeDumper):
#     def write_line_break(self, data=None):
#         super().write_line_break(data)
#         if len(self.indents) < 2:
#             super().write_line_break()


#  -------------------------
# RBAC
# -------------------------
# Roles are provided by each app (or global if app is Null)
# Roles are set of granular Permissions
# granular / atomic permission provided by each app
# p_xxx_permision[_c|_r|_u|_d|...]
# ex. p_home_access


class SirenePermission(models.Model):
    keyname = models.SlugField(_("Permission(*)"), max_length=128, blank=False, unique=True)
    displayname = models.CharField(_("Display name"), max_length=500, blank=True, default="")
    description = models.CharField("Description", max_length=500, blank=True, default="")
    is_builtin = models.BooleanField(_("Built-in permission"), default=False)

    class Meta:
        db_table = "iam_permission"
        ordering = ["keyname"]
        verbose_name = "Sirene Permission"
        verbose_name_plural = "Sirene Permissions"
        indexes = [
            models.Index(fields=["keyname"]),
            # models.Index(fields=["appname"]),
        ]

    def __str__(self):
        return f"{self.keyname}"


# -----------------------------------------------------------------------------
# Sirene Visitor
# ------------------------------------------------------------------------------
# class SireneVisitor(models.Model):
#     username = models.CharField(_("Username"), max_length=255, unique=True, blank=False)
#     user_ip = models.CharField(_("Visitor IP"), max_length=64, default="")
#     last_login = models.DateTimeField("Last Login", auto_now_add=False, blank=True, null=True)

#     class Meta:
#         db_table = "iam_visitor"
#         ordering = ["last_login"]
#         verbose_name = "Sirene Visitor"
#         verbose_name_plural = "Sirene Visitors"
#         indexes = [
#             models.Index(fields=["username"]),
#         ]

#     def __str__(self):
#         return self.username
