<?php

namespace App\Http\Middleware;

use App\Services\Response\ResponseService;
use Closure;
use App\Models\Device;
use Illuminate\Support\Facades\Auth;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class TokenCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $secret = '0b2a5f5606cdoftnmlccc664w89qtukfdbvvc19f585821g43rdf22';
        $header_secret = $request->header('secret');
        if($header_secret != $secret)
        {
            ResponseService::response([
                'status'=>401,
                'data'=>'',
                'message'=>'connection error'
            ]);
        }

        $device_token = $request->header('tokenDevice');
        if(!$device_token)
        {
            ResponseService::response([
                'status'=>401,
                'data'=>'',
                'message'=>'token device missed',
            ]);
        }

        $lang = $request->header('lang') ?? 'ar';
        $availabLelocals = array_keys(config('laravellocalization.supportedLocales') ?? []);
        $lang = in_array($lang,$availabLelocals) ? $lang : LaravelLocalization::getCurrentLocale();
        LaravelLocalization::setLocale($lang);


        return $next($request);
    }
}
