from django.db.models.signals import post_migrate, post_save
from django.dispatch import receiver

from .models import User
from .rbac import ensure_role_groups, sync_user_role_group


@receiver(post_migrate)
def bootstrap_role_groups(sender, **kwargs):
    if sender.label != "accounts":
        return
    ensure_role_groups()


@receiver(post_save, sender=User)
def sync_user_role_groups(sender, instance, **kwargs):
    sync_user_role_group(instance)
