<?php

declare(strict_types=1);

namespace App\Http\Middleware;

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

/**
 * Stops someone reaching the booking screen without the details a booking
 * cannot be made without.
 *
 * The artist name feeds the payment reference the studio matches against its
 * bank statement, and the date of birth gates two products. Collecting them
 * halfway through checkout, after a slot is already held, is the worst
 * possible moment — so the gate sits in front of the flow instead.
 */
class EnsureProfileIsComplete
{
    public function handle(Request $request, Closure $next): Response
    {
        $profile = $request->user()?->profile;

        if ($profile === null || ! $profile->isComplete()) {
            return redirect()
                ->route('profile.edit')
                ->with('status', 'Just a few details before your first booking.');
        }

        return $next($request);
    }
}
