<?php

declare(strict_types=1);

namespace Fennec\Console\Command;

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

/**
 * Génère le fichier de cache de routes, pour éviter un scan Reflection à chaque requête.
 */
final class RouteCacheCommand implements CommandInterface
{
    public function __construct(
        private readonly RouteScanner $scanner,
        private readonly RouteCache $cache,
        private readonly ConsoleOutput $output,
        private readonly string $controllerNamespace,
        private readonly string $controllerDirectory,
    ) {
    }

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

    public function getDescription(): string
    {
        return 'Génère le fichier de cache de routes.';
    }

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

        $this->output->success(\sprintf('%d route(s) mise(s) en cache.', \count($definitions)));

        return 0;
    }
}
