<?php

// app/Http/Middleware/RedirectIfAuthenticatedByRole.php
namespace App\Http\Middleware;

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

class RedirectIfAuthenticatedByRole
{
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            $user = Auth::user();

            if ($user->hasRole('super_admin')) return redirect()->route('superadmin.dashboard');
            if ($user->hasRole('admin')) return redirect()->route('admin.dashboard');
            if ($user->hasRole('student')) return redirect()->route('student.dashboard');
            if ($user->hasRole('lecturer')) return redirect()->route('lecturer.dashboard');
            if ($user->hasRole('admissions')) return redirect()->route('admissions.dashboard');
            if ($user->hasRole('bursar')) return redirect()->route('bursar.dashboard');
            if ($user->hasRole('library')) return redirect()->route('library.dashboard');
            if ($user->hasRole('registrar')) return redirect()->route('registrar.dashboard');
        }

        return $next($request);
    }
}
