<?php

declare(strict_types=1);

namespace App\Http\Middleware;

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

final class EnsureAdminOrModerator
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();
        $role = strtolower((string) ($user->role ?? ''));

        if (! in_array($role, ['admin', 'moderator'], true)) {
            abort(Response::HTTP_FORBIDDEN, 'Forbidden.');
        }

        return $next($request);
    }
}
