<?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 returns the status and some useful informations
 * about the given website
 *
 * @package Opentalent\OtAdmin\Command
 */
class GetSiteStatusCommand 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:status")
            ->setDescription("Displays the current state of a given website")
            ->setHelp("This CLI command returns a status code representing the current state of a given website")
            ->addOption(
                'full',
                null,
                InputOption::VALUE_NONE,
                "Performs a full scan (with warnings)"
            )
            ->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 \Opentalent\OtCore\Exception\NoSuchWebsiteException
     * @throws \TYPO3\CMS\Extbase\Object\Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $org_id = $input->getArgument('organization-id');
        $full = $input->getOption('full');

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

        $status = $this->siteController->getSiteStatusAction($org_id, $full);

        $headers = [];
        $values = [];
        foreach ($status->toArray() as $key => $value) {
            $headers[] = $key;
            $value = json_encode(
                str_replace("\"", "'", $value),
                JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
            );
            $value = trim($value, "\"");
            $values[] = $value;
        }

        $io->horizontalTable($headers, [$values]);

        $io->success('');
        return 0;
    }

}
