<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Core\Auth;
use App\Jobs\ApiLogJob;
use Closure;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Tymon\JWTAuth\Exceptions\JWTException;

/**
 * Class Response
 * @package App\Http\Middleware
 */
class Response
{
    /**
     * ComplaintHandle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     * @throws \Exception
     */
    public function handle($request, Closure $next)
    {
        Config::set("version", $request->header("version", 1));
        Config::set("appid", $request->header("appid", null));
        // ios|android|wx|qq
        Config::set("platform", $request->header("platform"));
        if (98047 == $request->get('debug')) {
            DB::enableQueryLog();
            Config::set('app.debug', true);
        }

        $response = $next($request);

        if ($request->get('debug') == 98047) {
            foreach (DB::getQueryLog() as $value) {
                dump($value);
            }
        }

        return $response;
    }

    /**
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Http\Response  $response
     */
    public function terminate($request, $response)
    {
    }
}
