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

declare(strict_types=1);

use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\ConsoleRunner as MigrationsConsoleRunner;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Console\ConsoleRunner as ORMConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
use Firehed\Container\TypedContainerInterface;
use OpenEMR\Console\Command;
use Symfony\Component\Console\Application;

$container = require_once __DIR__ . '/bootstrap.php';
assert($container instanceof TypedContainerInterface);

if (php_sapi_name() !== 'cli') {
    echo 'This can only be run from the command line.';
    exit(1);
}

file_put_contents(
    'php://stderr',
    "WARNING: This CLI tool is experimental and in progress. You probably want bin/console.\n",
);

$application = new Application();
$application->addCommand($container->get(Command\InstallCommand::class));
$application->addCommand($container->get(Command\ShellCommand::class));
$application->addCommand($container->get(Command\Codes\UpdateMappingsCommand::class));

// Doctrine Migrations integration
MigrationsConsoleRunner::addCommands($application, $container->get(DependencyFactory::class));

// Doctrine ORM integration
ORMConsoleRunner::addCommands($application, new SingleManagerProvider(
    $container->get(EntityManagerInterface::class),
));
$application->run();
