<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Console\Commands;

use PpPhp\Core\Platform\Console\AbstractCommand;
use PpPhp\Core\Platform\Event\EventBus;

final class EventsDispatchCommand extends AbstractCommand
{
    public function name(): string { return 'events:dispatch'; }
    public function description(): string { return 'Dispatch a test event.'; }

    public function handle(array $arguments = []): int
    {
        $eventName = $arguments[0] ?? 'platform.test';
        $bus = new EventBus();
        $bus->listen($eventName, static function ($event): void {
            // no-op test listener
        });

        $envelope = $bus->dispatch($eventName, ['source' => 'console']);

        $this->title('PPphp Event Dispatch');
        $this->line('Event: ' . $eventName);
        $this->line('Handled: ' . count($envelope->handledBy()));
        $this->line('Status: ' . ($envelope->ok() ? 'OK' : 'ERROR'));

        return $envelope->ok() ? 0 : 1;
    }
}
