<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Artdarek\Toastr\Facades\Toastr;
use Symfony\Component\HttpFoundation\Response;

class PermisosDeGestion
{
    /**
     * Handle an incoming request.
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (!auth()->user()->hasPermissionTo('gestion')) {
            Toastr::error('No tiene permiso para entrar a esta seccion!')->push();
            return back();
        }
        if ($request->is('gestion/paginas')) {
            if (!auth()->user()->hasPermissionTo('gestion-paginas')) {
                Toastr::error('No tiene permiso para entrar a esta seccion!')->push();
                return back();
            }
            return $next($request);
        }
        return $next($request);
    }
}
