#!/usr/bin/env php
<?php

use OpenApi\Console\GenerateCommand;
use OpenApi\Undefined;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

if (class_exists(Undefined::class) === false) {
    if (file_exists(__DIR__.'/../vendor/autoload.php')) {  // cloned / dev environment?
        require_once(__DIR__.'/../vendor/autoload.php');
    } else {
        require_once(realpath(__DIR__.'/../../../').'/autoload.php');
    }
}

$input = new class extends ArgvInput
{
    public function hasParameterOption(array|string $values, bool $onlyParams = false): bool
    {
        // Skip the built-in version option check
        // thus the command can use it for its own purpose
        if (['--version', '-V'] === $values) {
            return false;
        }

        return parent::hasParameterOption($values, $onlyParams);
    }
};
$output = new ConsoleOutput();
// diagnostics go to stderr: without -o the document itself is written to stdout;
// notices show at normal verbosity, so a default run says what Cleanup removed
$logger = new ConsoleLogger($output->getErrorOutput(), [LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL]);
$app = new Application();

// Remove Symfony's built-in options that conflict with our command options:
// --version (-V): conflicts with our --version (VALUE_REQUIRED for OpenAPI spec version)
// --no-interaction (-n): conflicts with our --pattern (-n)
$definition = $app->getDefinition();
$options = $definition->getOptions();
unset($options['version'], $options['no-interaction']);
$definition->setOptions($options);

$app->addCommands([new GenerateCommand($logger)]);
$app->setDefaultCommand('openapi', true);
$app->run($input, $output);
