<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckRole
{
    public function handle(Request $request, Closure $next, ...$roles)
    {
        if (!$request->user()) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

        // Admin can access everything
        if ($request->user()->isAdmin()) {
            return $next($request);
        }

        // For non-admin users, check if they have the required role
        if (!in_array($request->user()->role, $roles)) {
            return response()->json(['error' => 'Forbidden. You do not have the required role.'], 403);
        }

        return $next($request);
    }
} 