<?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
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->foreignId('product_bundle_id')->nullable()->after('product_serial_id')->constrained('product_bundles')->nullOnDelete();
            // Shared by every exploded component line from one bundle-add so the
            // UI can group them under a single bundle header, independent of row order.
            $table->string('bundle_group_key', 26)->nullable()->after('product_bundle_id')->index();
        });
    }

    public function down(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->dropColumn('bundle_group_key');
            $table->dropConstrainedForeignId('product_bundle_id');
        });
    }
};
