<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Which version the customer was actually looking at when they paid.
 *
 * A checkout is not instant: the session is created, the customer types their
 * card details, the webhook arrives. If a scheduled version transition happens
 * in that gap, resolving the plan by name at webhook time would contract them
 * to terms they never saw — a different price, a different quota — while their
 * payment was for the old ones.
 *
 * Stripe carries this through checkout metadata (phase 5). Until it does, the
 * column stays null and the webhook resolves the version on sale, which is the
 * behaviour it had anyway.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->foreignId('plan_version_id')->nullable()->after('plan')
                ->constrained()->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropConstrainedForeignId('plan_version_id');
        });
    }
};
