<?php

namespace App\Lexik;

use App\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(event: Events::JWT_CREATED, priority: 1)]
class JWTCreatedListener
{
    public function __invoke(JWTCreatedEvent $event): void
    {
        $user = $event->getUser();
        if (!$user instanceof User) {
            return;
        }

        $payload = $event->getData();

        $payload['id'] = $user->getId();
        $payload['username'] = $user->getUsername();
        $payload['uuid'] = $user->getUuid();
        $payload['roles'] = $user->getRoles();
        $payload['groupsView'] = $user->getGroupsView();

        $event->setData($payload);
    }
}