<?php

declare(strict_types=1);

namespace App\Framework\Notification\Channels\Telegram\Webhook;

/**
 * Telegram Callback Router
 *
 * Routes callback queries to registered handlers
 */
final class CallbackRouter
{
    /** @var array<string, CallbackHandler> */
    private array $handlers = [];

    /**
     * Register a callback handler
     */
    public function register(CallbackHandler $handler): void
    {
        $this->handlers[$handler->getCommand()] = $handler;
    }

    /**
     * Route callback query to appropriate handler
     *
     * @throws \RuntimeException if no handler found
     */
    public function route(TelegramCallbackQuery $callbackQuery): CallbackResponse
    {
        $command = $callbackQuery->getCommand();

        if (!isset($this->handlers[$command])) {
            throw new \RuntimeException("No handler registered for command: {$command}");
        }

        return $this->handlers[$command]->handle($callbackQuery);
    }

    /**
     * Check if a handler is registered for a command
     */
    public function hasHandler(string $command): bool
    {
        return isset($this->handlers[$command]);
    }

    /**
     * Get all registered commands
     *
     * @return array<string>
     */
    public function getRegisteredCommands(): array
    {
        return array_keys($this->handlers);
    }
}
