<?php

namespace Opentalent\OtAdmin\Command;


use Opentalent\OtAdmin\Controller\SiteController;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
 * This CLI command clears the cache of an existing organization's website
 *
 * By default, this command will only clear the frontend cache.
 * Pass the '-a / --all' option to clear all the typo3 caches.
 *
 * @package Opentalent\OtAdmin\Command
 */
class ClearSiteCacheCommand extends Command
{
    public function __construct(
        private readonly SiteController $siteController
    ) {}

    /**
     * -- This method is expected by Typo3, do not rename ou remove --
     *
     * Allows to configure the command.
     * Allows to add a description, a help text, and / or define arguments.
     *
     */
    protected function configure()
    {
        $this
            ->setName("ot:site:clear-cache")
            ->setDescription("Clear the cache of an organization website")
            ->setHelp("This CLI command clears the cache of an existing organization's website")
            ->addArgument(
                'organization-id',
                InputArgument::REQUIRED,
                "The organization's id in the opentalent DB"
            )->addOption(
                'all',
                'a',
                InputOption::VALUE_NONE,
                'Use this option to clear all the typo3 caches, and not only the frontend one'
            );
    }

    /**
     * -- This method is expected by Typo3, do not rename ou remove --
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @throws \Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $org_id = $input->getArgument('organization-id');
        $clearAll = $input->getOption('all');

        $io = new SymfonyStyle($input, $output);

        $rootUid = $this->siteController->clearSiteCacheAction($org_id, $clearAll);

        $io->success(sprintf("The cache has been cleared for the website with root uid " . $rootUid . ""));
        return 0;
    }

}
