import secrets

from django.contrib.auth import get_user_model
from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import Member

User = get_user_model()


@receiver(post_save, sender=User)
def create_member_profile(instance, *args, **kwargs) -> None:
    """Creates a Member profile for a newly created user."""
    member, created = Member.objects.get_or_create(user=instance)

    if not member.access_token:
        member.access_token = secrets.token_urlsafe(64)
        member.save(update_fields=["access_token"])
