<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        // \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        // \App\Http\Middleware\EncryptCookies::class,
        // \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        // \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        // \App\Http\Middleware\VerifyCsrfToken::class,
        \App\Http\Middleware\MergeJwtBearer::class,
        \App\Http\Middleware\ApiDebugLogger::class
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        // 'auth' => \App\Http\Middleware\Authenticate::class,
        // 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        // 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'auth.token' => \App\Http\Middleware\AuthenticateToken::class,
        'auth.isLoggedIn' => \App\Http\Middleware\IsLoggedIn::class,
        'auth.isPaidUser' => \App\Http\Middleware\IsPaidUser::class,        
        'apiToken' => \App\Http\Middleware\ApiToken::class,        
        'apiGateway' => \App\Http\Middleware\ApiGateway::class
    ];

    public function handle($request)
    {
        try
        {
            return parent::handle($request);
        }
        catch(NotFoundHttpException $e)
        {
            // if the request is ajax, then returns a json format
            if ($request->ajax())
            {
                return response()->json(['error' => $e->getMessage()], 404);
            }

            // or a view if it is a normal http request
            return response()->view('views.errors.404', [], 404);
        }
        catch (Exception $e)
        {
            $this->reportException($e);

            return $this->renderException($request, $e);
        }
    }

}
