<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

final class AdminRole
{
    public function handle(Request $request, Closure $next, ...$roles)
    {
        $ctx = (array) session('admin_ctx', []);
        $roleNames = (array)($ctx['role_names'] ?? []);

        if (in_array('super_admin', $roleNames, true)) {
            return $next($request);
        }

        if (empty($roles)) {
            return $next($request);
        }

        // 하나라도 매칭되면 통과
        foreach ($roles as $r) {
            if (in_array($r, $roleNames, true)) {
                return $next($request);
            }
        }

        abort(403);
    }
}
