<?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 removes any redirection from the given domain
 *
 * @package Opentalent\OtAdmin\Command
 */
class RemoveRedirectionCommand extends Command
{
    public function __construct(
        private readonly SiteController $siteController
    ) {
        parent::__construct();
    }

    /**
     * -- 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:redirection:remove")
            ->setDescription("Remove any redirection from the given domain")
            ->setHelp("Remove any redirection from the given domain. If --hard is true, they will be permanently deleted.")
            ->addArgument(
                'from-domain',
                InputArgument::REQUIRED,
                "The source domain"
            )->addOption(
                'hard',
                null,
                InputOption::VALUE_NONE,
                "Permanently delete the records and files. Use with caution."
            );
    }

    /**
     * -- This method is expected by Typo3, do not rename ou remove --
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     * @throws \Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $fromDomain = $input->getArgument('from-domain');
        $hard = $input->getOption('hard');

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

        $count = $this->siteController->removeRedirectionsFrom($fromDomain, $hard);

        $actionName = $hard ? 'hardly-deleted' : 'deleted';
        $io->success(sprintf("$count existing redirections from $fromDomain have been $actionName"));
        return 0;
    }
}
