<?php

namespace App\Http\Middleware;

use App\Traits\ApiResponseTrait;
use Closure;
use Illuminate\Http\Request;

class RequestValidate
{
    use ApiResponseTrait;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     */
    public function handle(Request $request, Closure $next)
    {
        if(!empty($request->header('security-key'))) {
            /*$json = customDecrypt($request->header('security-key'));
            $json_decoded = json_decode($json,true);

            if(isset($json_decoded['user_id']) && $json_decoded['user_id'] > 0){
               // $request->headers['user_id'] = $json_decoded['user_id'];
                setUserId($json_decoded['user_id']);
                return $next($request);
            }*/
            //dd($request->header('security-key'));
            setSecurityKey($request->header('security-key'));
            return $next($request);
        }

        return $this->sendApiResponse(
            [],
            "Unauthorized request",
            400,
            419
        );

    }
}
