<?php

namespace App\Http\Middleware;

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

class EnsureAccessLevel
{
    public function handle(Request $request, Closure $next, string ...$levels): Response
    {
        $user = $request->user();

        if (! $user || (! $user->isMasterAdmin() && ! in_array($user->access_level->value, $levels, true))) {
            return response()->json([
                'success' => false,
                'code' => 403,
                'message' => 'Anda tidak memiliki level akses yang diperlukan.',
            ], 403);
        }

        return $next($request);
    }
}
