<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class IsMentor
{
    /**
     * @param  Closure(Request): (Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (! $request->user()?->isMentor()) {
            if ($request->user()?->isAdmin()) {
                return redirect()->route('admin.dashboard')->with('error', 'Only mentors can access the mentor dashboard.');
            }

            if ($request->user()?->isSupportTeam()) {
                return redirect()->route('support-team.dashboard')->with('error', 'Only mentors can access the mentor dashboard.');
            }

            return redirect()->route('dashboard')->with('error', 'Only mentors can access the mentor dashboard.');
        }

        return $next($request);
    }
}
