<?php

namespace App\Http\Middleware;

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

class CheckFirstLoginCompleted
{
    public function handle(Request $request, Closure $next)
    {
        $user = Auth::user();

        if (!$user) {
            return $next($request);
        }

        if (!$user->first_login_completed) {
            if (!$request->routeIs(['first-login', 'logout', 'login'])) {
                return redirect('/first-login')
                    ->with('warning', 'Devi completare il tuo profilo prima di accedere alla piattaforma.');
            }
        }

        return $next($request);
    }
}
