<?php

namespace App\Http\Middleware;

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

/**
 * After login, NUDGE the operator to rotate the seeded password — but do not force it. The prompt
 * shows once per session and can be skipped (PasswordChange::skip sets `onboarding.password_skipped`);
 * a persistent banner keeps warning while the initial password is still in use. 2FA is optional too,
 * so it is never forced here.
 */
class EnsureSecurityOnboarded
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();

        if ($user && $user->must_change_password
            && ! $request->session()->get('onboarding.password_skipped')
            && ! $request->routeIs('password.change', 'logout')) {
            return redirect()->route('password.change');
        }

        return $next($request);
    }
}
