<?php

namespace App\Http\Middleware;

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

class RedirectIfAuthenticated
{
    /**
     * If user is authenticated, redirect to proper dashboard
     */
    public function handle(Request $request, Closure $next, ...$guards)
    {
        if (Auth::check()) {
            $user = Auth::user();

            // role অনুযায়ী redirect
            return redirect()->route(
                $user->role === 'admin' ? 'admin.dashboard' : 'dashboard'
            );
        }

        // guest হলে স্বাভাবিকভাবে চলবে (login/register দেখা যাবে)
        return $next($request);
    }
}
