<?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 undoes the soft-deletion an existing organization's website and updates it
 *
 * @package Opentalent\OtAdmin\Command
 */
class UndeleteSiteCommand 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:site:undelete")
            ->setDescription("Undo the soft-deletion of an organization website and update it")
            ->setHelp("Call this method by giving it the organization's id in the Opentalent DB. 
                            If an organization website has been deleted with the ot:site:delete
                            command and without the --hard option, you can undo the deletion with
                            this command.")
            ->addArgument(
                'organization-id',
                InputArgument::REQUIRED,
                "The organization's id in the opentalent DB"
            );
    }

    /**
     * -- 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): int
    {
        $org_id = $input->getArgument('organization-id');

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

        $rootUid = $this->siteController->undeleteSiteAction($org_id);
        $this->siteController->updateSiteAction($org_id);

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