<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class UserMiddleware
{
    /**
     * Only role = 'user' may access client/user-dashboard routes.
     * Admin accounts are redirected to the admin dashboard.
     * Guests are redirected to login.
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (!Auth::check()) {
            return redirect()->route('login');
        }

        if (Auth::user()->isAdmin()) {
            // Admin accidentally hit the user dashboard — send them to admin panel
            return redirect()->route('dashboard');
        }

        return $next($request);
    }
}
