<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Console\Commands;

use PpPhp\Core\Platform\Console\AbstractCommand;

final class CommandBusDispatchCommand extends AbstractCommand
{
    public function name(): string { return 'command:dispatch'; }
    public function description(): string { return 'PPphp Command Dispatch'; }

    public function handle(array $arguments = []): int
    {
        $this->title('PPphp Command Dispatch');
        $runtime = \PpPhp\Core\Platform\Event\Sourcing\Command\CommandBusRuntime::instance(\PpPhp\Core\Platform\Console\ConsolePath::basePath());
        $runtime->register(\PpPhp\Core\Platform\Event\Sourcing\Command\CommandHandlerDefinition::create('console_command_handler', 'ConsoleCommand', 'ConsoleCommandHandled'));
        $result = $runtime->dispatch(\PpPhp\Core\Platform\Event\Sourcing\Command\CommandMessage::create('ConsoleCommand', 'console-command-aggregate', 'ConsoleAggregate', ['handled' => true], ['source' => 'console']));
        $this->line('Handled: ' . ($result->isHandled() ? 'YES' : 'NO'));
        $this->line('Successful: ' . ($result->successful() ? 'YES' : 'NO'));
        return 0;
    }
}
