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

use AWurth\Silex\User\Command\ActivateUserCommand;
use AWurth\Silex\User\Command\ChangePasswordCommand;
use AWurth\Silex\User\Command\CreateUserCommand;
use AWurth\Silex\User\Command\DeactivateUserCommand;
use AWurth\Silex\User\Command\DemoteUserCommand;
use AWurth\Silex\User\Command\PromoteUserCommand;
use Saxulum\Console\Provider\ConsoleProvider;
use App\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

if (!isset($_SERVER['APP_ENV'])) {
    if (!class_exists(Dotenv::class)) {
        throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
    }
    (new Dotenv())->load(__DIR__.'/../.env');
}

$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev');
$debug = ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption(['--no-debug', '']);

if ($debug) {
    Debug::enable();
}

$app = new Application($env, $debug);

$app['debug'] = $debug;
$app['env'] = $_SERVER['APP_ENV'] ?? 'dev';
$app['root_dir'] = dirname(__DIR__);

$app->register(new ConsoleProvider());

$app['console']->add(new ActivateUserCommand($app));
$app['console']->add(new ChangePasswordCommand($app));
$app['console']->add(new CreateUserCommand($app));
$app['console']->add(new DeactivateUserCommand($app));
$app['console']->add(new DemoteUserCommand($app));
$app['console']->add(new PromoteUserCommand($app));

$app->boot();
$app['console']->run();
