<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Compiler\Stages;

use PpPhp\Core\Platform\Event\Compiler\EventArtifactWriter;
use PpPhp\Core\Platform\Event\Compiler\EventCompilerContext;
use PpPhp\Core\Platform\Event\Compiler\EventCompilerPaths;
use PpPhp\Core\Platform\Event\Compiler\EventCompilerStageInterface;
use PpPhp\Core\Platform\Event\Lifecycle\LifecycleEventManifest;

final class EventManifestCompileStage implements EventCompilerStageInterface
{
    public function __construct(private readonly EventArtifactWriter $writer = new EventArtifactWriter()) {}

    public function name(): string { return 'event-manifest'; }

    public function compile(EventCompilerContext $context): void
    {
        $registry = LifecycleEventManifest::registry();
        $data = [
            'generatedAt' => date('c'),
            'count' => $registry->count(),
            'categories' => $registry->categories(),
            'events' => $registry->all(),
        ];
        $this->writer->writePhpArray(EventCompilerPaths::cacheFile($context->basePath(), 'compiled-event-manifest.php'), $data);
        $context->setArtifact('eventManifest', $data);
    }
}
