<?php

namespace App\Http\Middleware;

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

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     */
    public function handle(Request $request, Closure $next): Response
    {
        $user = session('user');
        $role = is_array($user) ? ($user['role'] ?? null) : ($user->role ?? null);

        if (!$user || $role !== 'admin') {
            return redirect()->route('admin.login');
        }

        return $next($request);
    }
}
