<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckMenuAccess
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $menuRoute
     * @return mixed
     */
    public function handle(Request $request, Closure $next, $menuRoute)
    {
        // Cek apakah user sudah login
        if (!auth()->check()) {
            return redirect()->route('login');
        }

        // Cek apakah user memiliki akses ke menu ini
        if (!auth()->user()->canAccessMenu($menuRoute)) {
            // Tampilkan halaman error 403 custom
            return response()->view('errors.403', [], 403);
        }

        return $next($request);
    }
}