<?php

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'app:maintenance')]
class MaintenanceCommand extends Command
{
    private $projectDir;

    public function __construct(string $projectDir)
    {
        parent::__construct();
        $this->projectDir = $projectDir;
    }

    protected function configure(): void {
        $this->addArgument('status', InputArgument::REQUIRED, 'on ou off');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $status = $input->getArgument('status');
        $maintenanceFile = $this->projectDir . '/var/.maintenance';

        if ($status === 'on') {
            touch($maintenanceFile);
            $io->success('Mode maintenance ACTIVÉ (Fichier var/.maintenance créé)');
        } else {
            if (file_exists($maintenanceFile)) {
                unlink($maintenanceFile);
            }
            $io->success('Mode maintenance DÉSACTIVÉ (Fichier var/.maintenance supprimé)');
        }

        return Command::SUCCESS;
    }
}
