<?php

declare(strict_types=1);

namespace App\Util;

use App\Scenario\ScenarioInterface;
use Pkirillw\MaxBotApi\Scheme\Update\MessageCreatedUpdate;

final class CommandRouter
{
    /** @var array<string, ScenarioInterface> */
    private array $scenarios = [];

    public function add(string $commandName, ScenarioInterface $scenario): void
    {
        $this->scenarios[$commandName] = $scenario;
    }

    public function route(MessageCreatedUpdate $update, ParsedCommand $command): bool
    {
        $scenario = $this->scenarios[$command->name] ?? null;
        if ($scenario === null) {
            return false;
        }
        $scenario->handle($update, $command);
        return true;
    }

    /**
     * @return list<string>
     */
    public function commands(): array
    {
        return array_keys($this->scenarios);
    }
}
