<?php

declare(strict_types=1);

namespace App\Module\Core\Infrastructure\Audit;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Uid\Uuid;

/**
 * Provides an HTTP request identifier (UUID v4) shared by every audit row
 * produced during a single main request. Null in CLI (fixtures, batch).
 */
final class RequestIdProvider
{
    private ?string $requestId = null;

    #[AsEventListener(event: 'kernel.request')]
    public function onKernelRequest(RequestEvent $event): void
    {
        if (!$event->isMainRequest()) {
            return;
        }

        $this->requestId = Uuid::v4()->toRfc4122();
    }

    public function getRequestId(): ?string
    {
        return $this->requestId;
    }
}
