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

declare(strict_types=1);

namespace Php\Pie;

use Composer\InstalledVersions;
use Php\Pie\Command\BuildCommand;
use Php\Pie\Command\DownloadCommand;
use Php\Pie\Command\InfoCommand;
use Php\Pie\Command\InstallCommand;
use Php\Pie\Command\ShowCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/** @psalm-suppress UnresolvableInclude */
include $_composer_autoload_path ?? '/usr/share/pie/vendor/autoload.php';

$container = Container::factory();

$application = new Application(
    '🥧 PHP Installer for Extensions (PIE)',
    (static function (): string {
        $pieVersion = '0.2.0';

        /**
         * @psalm-suppress RedundantCondition
         * @noinspection PhpConditionAlreadyCheckedInspection
         */
        if ($pieVersion === '@pie_version'.'@') {
            if (!class_exists(InstalledVersions::class)) {
                /**
                 * Note: magic constant that causes Symfony Console to not display a version
                 * {@see Application::getLongVersion()}
                 */
                return 'UNKNOWN';
            }

            $installedVersion = InstalledVersions::getVersion(InstalledVersions::getRootPackage()['name']);
            if ($installedVersion === null) {
                return 'UNKNOWN';
            }

            return $installedVersion;
        }

        /** @psalm-suppress NoValue */
        return $pieVersion;
    })()
);
$application->setCommandLoader(new ContainerCommandLoader(
    $container,
    [
        'download' => DownloadCommand::class,
        'build' => BuildCommand::class,
        'install' => InstallCommand::class,
        'info' => InfoCommand::class,
        'show' => ShowCommand::class,
    ]
));
$application->run($container->get(InputInterface::class), $container->get(OutputInterface::class));
