<?php

namespace App\Http\Middleware;

use App\Exceptions\DomainException;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class RoleMiddleware
{
    public function handle(Request $request, Closure $next, string ...$roles): Response
    {
        $role = $request->user()?->role?->value;

        if (! $role || ! in_array($role, $roles, true)) {
            throw new DomainException('FORBIDDEN', 'You are not authorized to access this resource.', 403);
        }

        return $next($request);
    }
}
