from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _


class UserProfile(models.Model):
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        primary_key=True,
    )

    is_moderation_expert = models.BooleanField(
        verbose_name=_("Expert moderation user"),
        help_text=_(
            "This user is an expert in moderation, and the interface features additional functionalities."
        ),
        default=False,
    )

    class Meta:
        verbose_name = _("User profile")
        verbose_name_plural = _("User profiles")

    def __str__(self):
        return _("User profile") + " (" + self.user.username + ")"


@receiver(post_save, sender=User)
def update_profile_signal(sender, instance, created, **kwargs):
    if not hasattr(instance, "userprofile"):
        UserProfile.objects.create(user=instance)

    instance.userprofile.save()
