<?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\Runtime\EventRuntimeRegistry;

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

    public function name(): string { return 'listener-metadata'; }

    public function compile(EventCompilerContext $context): void
    {
        $runtime = new EventRuntimeRegistry();
        $runtime->registry()->register('compiler.listener.test', static function (): void {}, 100, ['compiler']);
        $data = [
            'generatedAt' => date('c'),
            'summary' => $runtime->summary(),
            'registry' => $runtime->registry()->toArray(),
        ];
        $this->writer->writePhpArray(EventCompilerPaths::cacheFile($context->basePath(), 'compiled-listener-metadata.php'), $data);
        $context->setArtifact('listenerMetadata', $data);
    }
}
