<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class IsAdmin
{
    public function handle(Request $request, Closure $next)
    {
        // PERBAIKAN: Cek kolom 'is_admin', BUKAN 'role'
        // Angka 1 artinya Admin, 0 artinya User biasa
        if (Auth::check() && Auth::user()->is_admin == 1) {
            return $next($request); // Silakan masuk bos
        }

        // Kalau bukan admin (is_admin = 0), tendang ke halaman depan
        return redirect('/')->with('error', 'Anda bukan Admin!');
    }
}
