<?php

declare(strict_types=1);

namespace Fennec\Console\Command;

use Fennec\Console\CommandInterface;
use Fennec\Console\ConsoleOutput;
use Fennec\Routing\RouteScanner;

/**
 * Liste les routes enregistrées (méthode, chemin, controller) pour faciliter le debug.
 */
final class RouteListCommand implements CommandInterface
{
    public function __construct(
        private readonly RouteScanner $scanner,
        private readonly ConsoleOutput $output,
        private readonly string $controllerNamespace,
        private readonly string $controllerDirectory,
    ) {
    }

    public function getName(): string
    {
        return 'route:list';
    }

    public function getDescription(): string
    {
        return 'Liste les routes enregistrées.';
    }

    public function execute(array $arguments): int
    {
        $definitions = $this->scanner->scanDirectory($this->controllerNamespace, $this->controllerDirectory);

        foreach ($definitions as $definition) {
            $this->output->info(\sprintf(
                '%-6s %-40s -> %s::%s',
                \implode('|', $definition->methods),
                $definition->path,
                $definition->controllerClass,
                $definition->controllerMethod,
            ));
        }

        $this->output->success(\sprintf('%d route(s) trouvée(s).', \count($definitions)));

        return 0;
    }
}
