<?php
declare(strict_types = 1);

namespace Couscous\Application\Cli;

use Couscous\Generator;
use Couscous\Model\Project;
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;

/**
 * Generates the website.
 *
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
 */
class GenerateCommand extends Command
{
    /**
     * @var Generator
     */
    private $generator;

    public function __construct(Generator $generator)
    {
        $this->generator = $generator;

        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setName('generate')
            ->setDescription('Generate the website')
            ->addArgument(
                'source',
                InputArgument::OPTIONAL,
                'Repository you want to generate.',
                getcwd()
            )
            ->addOption(
                'target',
                null,
                InputOption::VALUE_REQUIRED,
                'Target directory in which to generate the files.',
                getcwd().'/.couscous/generated'
            )
            ->addOption(
                'config-file',
                'f',
                InputOption::VALUE_REQUIRED,
                'If specified, use the given file as configuration file.',
                'couscous.yml'
            )
            ->addOption(
                'config',
                null,
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
                'If specified will override entries in couscous.yml (key=value)',
                []
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        /** @var string */
        $source = $input->getArgument('source');
        /** @var string */
        $target = $input->getOption('target');
        /** @var string */
        $configFile = $input->getOption('config-file');
        /** @var array */
        $cliConfig = $input->getOption('config');

        $project = new Project($configFile, $source, $target);

        $project->metadata['cliConfig'] = $cliConfig;

        $this->generator->generate($project, $output);

        return 0;
    }
}
