<?php

namespace App\Http\Middleware;

use App\Services\PreferencesStore;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureOnboardingCompleted
{
    /**
     * Handle an incoming request.
     *
     * @param  Closure(Request): (Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (! auth()->check()) {
            return $next($request);
        }

        if (! (PreferencesStore::all()['onboarding']['completed'] ?? false)) {
            return redirect()->route('onboarding');
        }

        return $next($request);
    }
}
