<?php

namespace App\Framework\Core\Commands;

use App\Framework\Cache\Cache;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\ConsoleInput;
use App\Framework\Console\ConsoleOutput;
use App\Framework\Core\PathProvider;
use App\Framework\Debug\OutputInterface;

final readonly class ClearDiscoveryCache
{
    public function __construct(
        private Cache $cache,
        private PathProvider $pathProvider)
    {
    }

    #[ConsoleCommand('discovery:clear', 'Clears the discovery cache')]
    public function __invoke(): void
    {
        $this->cache->forget('discovery_service');
    }

    #[ConsoleCommand('clear:routes', 'Clears the routes cache')]
    public function clearRoutes(ConsoleInput $input, ConsoleOutput $output): void
    {
        if(file_exists($this->pathProvider->resolvePath('/cache/routes.cache.php')) === false) {
            $output->writeError('Routes cache not found');
            return;
        }

        unlink($this->pathProvider->resolvePath('/cache/routes.cache.php'));

        $output->writeSuccess('Routes cache cleared');;
    }
}
