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

$projectRoot = dirname(dirname(dirname(dirname(__DIR__)))) . '';
chdir($projectRoot);

if (!defined('APPLICATION_PATH')) {
    define('APPLICATION_PATH', dirname(__DIR__) . '/');
}

/**
 *  Now whit environment be this?
 */
if (!defined('APPLICATION_ENV') && getenv('APPLICATION_ENV')) {
    define('APPLICATION_ENV', (getenv('APPLICATION_ENV')
        ? getenv('APPLICATION_ENV')
        : 'production'));
}

/**
 *
 * I be autoloadin' th'composer or else shiver me timbers
 *
 */
if (!file_exists('vendor/autoload.php')) {
    throw new RuntimeException(
        'Garrrr! Unable t\'load Bone. Run `composer install` or `php composer.phar install`'
    );
}

require_once 'vendor/autoload.php';

use Bone\Console\ConsoleApplication;
use Bone\OAuth2\Entity\OAuthUser;
use Bone\OAuth2\Repository\ScopeRepository;
use Bone\OAuth2\Service\ClientService;
use Del\Console\UserCommand;
use Bone\OAuth2\Command\ClientCommand;
use Bone\OAuth2\Command\ClientScopeCommand;
use Bone\OAuth2\Command\ScopeCreateCommand;
use Bone\OAuth2\Command\ScopeListCommand;
use Del\Service\UserService;


$container = Bone\Mvc\Application::ahoy()->bootstrap();

// Override the Del\Entity\User with our OAuth user class
/** @var UserService $userService */
$userService = $container[UserService::class];
$userService->setUserClass(OAuthUser::class);

// Set up the application
$app = new ConsoleApplication();

$userCommand = new UserCommand($userService);
$userCommand->setName('user:reset-pass');
$clientService = $container->get(ClientService::class);
$scopeRepository = $container->get(ScopeRepository::class);

$clientCommand = new ClientCommand($clientService, $userService, $scopeRepository);
$scopeCreateCommand = new ScopeCreateCommand($scopeRepository);
$scopeListCommand = new ScopeListCommand($scopeRepository);
$clientScopeCommand = new ClientScopeCommand($clientService, $scopeRepository);

$app->add($userCommand);
$app->add($clientCommand);
$app->add($scopeCreateCommand);
$app->add($scopeListCommand);
$app->add($clientScopeCommand);

$app->run();
