<?php

namespace App\Http\Middleware;

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

class RequirePasswordChange
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();

        if (
            $user
            && $user->tenant_id
            && $user->must_change_password
            && ! $request->routeIs('app.profile', 'app.profile.update', 'app.profile.password', 'logout')
        ) {
            return redirect()
                ->route('app.profile')
                ->with('warning', 'For your security, change the temporary password before continuing.');
        }

        return $next($request);
    }
}
