<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class WaiterMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  Closure(Request): (Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $user = auth()->user();

        if (! $user || $user->role !== 'waiter' || ! $user->active) {
            auth()->logout();

            return redirect()->route('waiter.login')->withErrors(['email' => 'Unauthorized access to Waiter Panel.']);
        }

        return $next($request);
    }
}
