<?php

namespace App\Http\Middleware;

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

class AdminMiddleware
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();
        if (! $user) {
            return redirect()->route('login');
        }
        $role = $user->role ?? ($user->is_admin ? 'admin' : 'viewer');
        if (! in_array($role, ['admin', 'editor', 'viewer'], true)) {
            return redirect()->route('login');
        }

        return $next($request);
    }
}
