<?php

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

(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new App\Core\Application(
    dirname(__DIR__)
);

$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config');
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage');
$app->withFacades();

$app->alias('cache', \Illuminate\Cache\CacheManager::class);
$app->alias('auth', \Illuminate\Auth\AuthManager::class);

$app->withEloquent();

$app->configure('settingConfig');

$app->configure('website');

$app->configure('services');

$app->configure('jwt');
/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->configure('cors');
$app->register(Barryvdh\Cors\ServiceProvider::class);
$app->middleware([
    // ...
    \Barryvdh\Cors\HandleCors::class,
]);

$app->singleton(
    'filesystem',
    function ($app) {
        return $app->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem');
    });

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

$app->routeMiddleware([
    'authenticate' => App\Http\Middleware\AuthenticateJWT::class,
    'authenticated' => App\Http\Middleware\AuthenticatedJWT::class,
    'authenticated_if_exist' => App\Http\Middleware\AuthenticatedJWTIfExist::class,
    'cors' => App\Http\Middleware\CorsMiddleware ::class,
    'localization' => App\Http\Middleware\LocalizationMiddleware::class,
    'app_localization' => App\Http\Middleware\AppLocalization::class,
    'is_merchant' => App\Http\Middleware\MerchantMiddleware::class,
    'validateAdminReferer' => \App\Http\Middleware\ValidateAdminRefererMiddleware::class,
    'throttle' => \App\Http\Middleware\ThrottleRequests::class,
    'partner_auth' => \App\Http\Middleware\ValidatePartnerAuth::class
]);

app('translator')->setLocale('ar');

$app->middleware([
    App\Http\Middleware\ForceUpdateMiddleware::class,
    App\Http\Middleware\FixPostMiddleware::class,
]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
$app->register(Yajny\EventsYj\EventsYjServiceProvider::class);
$app->register(Yajny\MailYj\MailYjServiceProvider::class);
$app->register(\Rollbar\Laravel\RollbarServiceProvider::class);
$app->register(Illuminate\Redis\RedisServiceProvider::class);

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__ . '/../routes/web.php';
    require __DIR__ . '/../routes/app.php';
});

// TO send emails
$app->configure('mail');
$app->alias('mailer', Illuminate\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\MailQueue::class);

$app->register(Illuminate\Mail\MailServiceProvider::class);

// Generate Commands
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);

// to queue sending emails
$app->make('queue');

return $app;
