<?php

namespace App\Http\Middleware;

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

class RoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next, $role): Response
    {
        if ($request->routeIs('staff.surveys.soalan')) {
            return $next($request);
        }

        if (!auth::check()) {
            return redirect()->route('login');
        }

        if (Auth::user()->role != $role) {
            abort(403, 'Tak boleh la begitu ji.');

        }
        return $next($request);
    }
}
