<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Console;

use PpPhp\Core\Platform\Kernel\Platform;

final class PlatformConsole
{
    private CommandRegistry $registry;
    private CommandBus $bus;

    public function __construct(private readonly string $basePath)
    {
        Platform::reset();
        Platform::boot($this->basePath);

        $this->registry = new CommandRegistry();
        (new PlatformCommandProvider())->register($this->registry);
        $this->bus = new CommandBus($this->registry);
    }

    public function run(array $argv): int
    {
        $command = $argv[1] ?? 'about';

        if (in_array($command, ['help', '--help', '-h'], true)) {
            return $this->bus->help();
        }

        return $this->bus->dispatch($command, array_slice($argv, 2));
    }

    public function registry(): CommandRegistry
    {
        return $this->registry;
    }
}
