<?php

namespace App\Http\Middleware;

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

class RoleMiddleware
{
    public function handle(Request $request, Closure $next, ...$roles): Response
    {
        // Agar user login nahi hai, ya uska role allowed roles mein nahi hai
        if (!Auth::check() || !in_array(Auth::user()->role, $roles)) {
            // Usko wapas home page par bhej do error ke sath
            return redirect('/')->with('error', 'Unauthorized access. You do not have permission to view this page.');
        }

        return $next($request);
    }
}