#!/usr/bin/env php
<?php declare(strict_types=1);

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\PackageBuilder\Configuration\ConfigFilePathHelper;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\Statie\Console\Application;
use Symplify\Statie\DependencyInjection\ContainerFactory;

// performance boost
gc_disable();

require_once __DIR__ . '/statie-bootstrap.php';

try {
    // 1. Detect configuration
    ConfigFilePathHelper::detectFromInput('statie', new ArgvInput());

    // 2. Build DI container
    $containerFactory = new ContainerFactory();
    $configFile = ConfigFilePathHelper::provide('statie', 'statie.yml');

    if ($configFile) {
        $container = $containerFactory->createWithConfig($configFile);
    } else {
        $container = $containerFactory->create();
    }

    // 3. Run Console Application
    /** @var Application $application */
    $application = $container->get(Application::class);
    /** @var InputInterface $input */
    $input = $container->get(InputInterface::class);
    /** @var OutputInterface $output */
    $output = $container->get(OutputInterface::class);
    exit($application->run($input, $output));
} catch (Throwable $throwable) {
    $symfonyStyle = SymfonyStyleFactory::create();
    $symfonyStyle->error($throwable->getMessage());
    exit(1);
}
