<?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 adds a redirection from a domain to another
 *
 * @package Opentalent\OtAdmin\Command
 */
class AddRedirectionCommand 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:add")
            ->setDescription("Add a redirection from a domain to another")
            ->setHelp("Add a redirection from a domain to another")
            ->addArgument(
                'from-domain',
                InputArgument::REQUIRED,
                "The domain to be redirected"
            )->addArgument(
                'to-domain',
                InputArgument::REQUIRED,
                "The target domain"
            );
    }

    /**
     * -- 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)
    {
        $fromDomain = $input->getArgument('from-domain');
        $toDomain = $input->getArgument('to-domain');

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

        $status = $this->siteController->addRedirection($fromDomain, $toDomain);

        if ($status == SiteController::REDIRECTION_UPDATED) {
            $io->success(sprintf("A existing redirection has been restored and updated"));
        } else {
            $io->success(sprintf("A new redirection has been added"));
        }
        return 0;
    }
}
