<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('order_payments')) {
            throw new RuntimeException('The order_payments table must exist before enabling customer advances.');
        }

        if (! Schema::hasColumn('order_payments', 'order_receipt_id')) {
            Schema::table('order_payments', function (Blueprint $table) {
                $table->unsignedBigInteger('order_receipt_id')->nullable()->after('order_id');
                $table->index('order_receipt_id', 'order_payments_receipt_id_index');
            });
        }
    }

    public function down(): void
    {
        // Keep receipt links intact when rolling back a live deployment batch.
    }
};
