from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from rules import is_superuser
from rules.contrib.models import RulesModel

from members.rules import is_member_manager


class MemberManager(models.Manager):
    def get_queryset(self) -> models.QuerySet:
        return super().get_queryset().select_related("user")


class Member(RulesModel):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="member", verbose_name=_("user"))
    family_members = models.ManyToManyField("self", symmetrical=True, blank=True, verbose_name=_("family members"))

    birthday = models.DateField(_("birthday"), blank=True, null=True)
    license = models.CharField(_("license"), max_length=20, blank=True, null=True)

    phone_number = PhoneNumberField(_("phone number"), blank=True, null=True)
    emergency_phone_number = PhoneNumberField(_("emergency phone number"), blank=True, null=True)

    access_token = models.CharField(_("access token"), max_length=255, blank=True, null=True)

    created = models.DateTimeField(auto_now_add=True, verbose_name=_("created"))
    updated = models.DateTimeField(auto_now=True, verbose_name=_("updated"))

    objects = MemberManager()

    class Meta:
        verbose_name = _("member")
        verbose_name_plural = _("members")
        ordering = ["user__last_name", "user__first_name"]
        permissions = [("member_manager", _("Can manage members"))]
        rules_permissions = {
            "add": is_superuser | is_member_manager,
            "change": is_superuser | is_member_manager,
            "delete": is_superuser | is_member_manager,
            "view": is_superuser | is_member_manager,
        }

    def __str__(self):
        return self.user.get_full_name()
