<?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 delete an existing organization's website
 *
 * @package Opentalent\OtAdmin\Command
 */
class DeleteSiteCommand 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:delete")
            ->setDescription("Delete an organization website")
            ->setHelp("Call this method by giving it the organization's id in the Opentalent DB. 
                            By default, it proceed to a soft deletion, marking the records as deleted 
                            and renaming files: this kind of deletion can then be undone 
                            with the ot:site:undelete command.
                            If the --hard option is passed, the records and files will be permanently deleted,
                            with no possibility of undoing. A confirmation will be demanded.")
            ->addOption(
                'hard',
                null,
                InputOption::VALUE_NONE,
                "Permanently delete the records and files. Use with caution."
            )
            ->addOption(
                'no-redirect',
                'd',
                InputOption::VALUE_REQUIRED,
                "If set, the website's url won't be redirected to its parent's website"
            )
            ->addOption(
                'force',
                'f',
                InputOption::VALUE_NONE,
                "Force the deletion of website directories, even if they are not empty (this has no effect without the --hard option)"
            )
            ->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)
    {
        $org_id = $input->getArgument('organization-id');
        $hard = $input->getOption('hard');
        $force = $input->getOption('force');
        $noRedirect = $input->getOption('no-redirect');

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

        $rootUid = $this->siteController->deleteSiteAction($org_id, $hard, !$noRedirect, $force);

        if ($hard) {
            $io->success(sprintf("The website with root uid " . $rootUid . " has been permanently deleted"));
        } else {
            $io->success(sprintf("The website with root uid " . $rootUid . " has been soft-deleted. Use ot:site:undelete to restore it."));
        }
        return 0;
    }

}
