<?php

namespace App\Http\Middleware;

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

class AdminMiddleware
{
   public function handle(Request $request, Closure $next)
{
    if (!Auth::check()) {
        return redirect()->route('login')->with('error', 'Silakan login terlebih dahulu');
    }

    if (Auth::user()->role !== 'admin') {
        // Redirect ke halaman sesuai role atau home umum
        return redirect()->route('dashboard.'.Auth::user()->role)
               ->with('error', 'Akses hanya untuk admin');
    }

    return $next($request);
}
}