<?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 creates an organization's website
 * by fetching its latest data from the Opentalent API
 *
 * @package Opentalent\OtAdmin\Command
 */
class CreateSiteCommand 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:create")
            ->setDescription("Create an organization's website " .
                                       "by fetching its latest data from the Opentalent API")
            ->setHelp("Call this method by giving it the organization's id in the Opentalent DB. 
                            If no site exists, it will create it; 
                            If a site already exists, it won't do anything.")
            ->addArgument(
                'organization-id',
                InputArgument::REQUIRED,
                "The organization's id in the opentalent DB"
            )->addOption(
                'dev',
                null,
                InputOption::VALUE_NONE,
                "Add this option to get url like 'http://host/subdomain' instead of 'http://subdomain.host'"
            );
    }

    /**
     * -- 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');

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

        $rootUid = $this->siteController->createSiteAction($org_id);

        $io->success(sprintf("A new website has been created with root page uid=" . $rootUid));
        return 0;
    }
}
