<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Console\Commands;

use PpPhp\Core\Platform\Console\AbstractCommand;
use PpPhp\Core\Platform\Console\ConsolePath;
use PpPhp\Core\Platform\Event\Compiler\ListenerRegistryCompiler;
use PpPhp\Core\Platform\Event\Runtime\EventRuntimeRegistry;

final class ListenersCompileCommand extends AbstractCommand
{
    public function name(): string { return 'listeners:compile'; }
    public function description(): string { return 'Compile listener registry cache.'; }

    public function handle(array $arguments = []): int
    {
        $runtime = new EventRuntimeRegistry();
        $runtime->registry()->register('listeners.compile.test', static function (): void {}, 0, ['compile']);

        $data = (new ListenerRegistryCompiler())->compile($runtime->registry(), ConsolePath::basePath());

        $this->title('PPphp Listener Registry Compile');
        $this->line('Events: ' . count($data['events']));
        $this->line('Listeners: ' . ($data['listenerCount'] ?? 0));
        $this->line('Output: ' . ListenerRegistryCompiler::CACHE_FILE);
        return 0;
    }
}
