<?php

namespace App\Http\Middleware;

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

class RoleCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    // public function handle(Request $request, Closure $next, ...$roles)
    // {
    //     foreach ($roles as $role) {
    //         if (Auth::check() && (in_array(Auth::user()->role, $roles) || in_array(Auth::user()->role, ['pemerintah', 'investor', 'akademisi', 'umkm', 'komunitas']))) {
    //             return $next($request);
    //         }            
    //     }

    //     Auth::logout();
    //     return redirect()->route('login')->with('status', 'You are not authorized to access this page.');
    // }

    public function handle(Request $request, Closure $next, ...$roles)
    {
        if (Auth::check() && in_array(Auth::user()->role, $roles)) {
            return $next($request);
        }

        Auth::logout();
        return redirect()->route('login')->with('status', 'You are not authorized to access this page.');
    }
}
