<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class CheckAccountStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->status == 0) {
            Auth::logout();
            $notification = [
                'status' => 'Error',
                'message' => 'Your account has been disabled. Please contact support for assistance.',
            ];
            return redirect()->route('login')->with($notification);
        }

        return $next($request);
    }
}
