<?php

namespace App\EventLog\Framework\Repository;

use App\EventLog\Framework\Entity\EventLog;
use App\User\Framework\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<EventLog>
 */
class EventLogRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, EventLog::class);
    }

    public function insert(
        User $user,
        string $type,
        string $message,
        array $context = []
    ): EventLog {
        $eventLog = new EventLog()
            ->setUser($user)
            ->setType($type)
            ->setMessage($message)
            ->setContext($context);
        $this->getEntityManager()->persist($eventLog);
        $this->getEntityManager()->flush();
        return $eventLog;
    }
}
