<?php

namespace App\Http\Middleware;

use Closure;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        $allowedRoles = ['admin', 'agents', 'ppc', 'finance', 'team-coordinator'];
        
        if ($request->user() && in_array($request->user()->role, $allowedRoles)) {
            // if ($request->user()->role !== 'admin' && $request->route()->getName() !== 'leads.index') {
            //     return redirect()->route('leads.index');
            // }
            return $next($request);
        }
    
        if (!$request->user()) {
            return redirect()->route('login'); // Redirect to login if user is not authenticated
        }
    
        return redirect()->back()->with('error', 'You do not have access to the page');
    }
    
}
