<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;

class FrontAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (Auth::check() == true && Auth::user()->role_id == 2) {
            if(Auth::user()->is_block == 1){
                Auth::user()->update([
                    'is_active' => 0
                ]);
                Auth::logout();
                
                return redirect(route('front.login'))->with('error', 'Your account is temporary suspended');
            }else if(Auth::user()->has_company == 0){
                return redirect(route('front.addCompanyDetail'));
            }else if(Auth::user()->status == 0){
                return redirect(route('front.otp').'?is_register=true&token='.Auth::user()->remember_token.'&id='.Auth::id());
            }
            return $next($request);
        }
        return redirect(route('front.login'));
    }
}
