<?php

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

/**
 * Link a transfer line to a registered asset (Release 2). Consumables keep
 * moving ERP stock via product_id/variation_id; asset lines now carry an
 * asset_id so acceptance moves the asset's location through AssetService and
 * writes a movement audit row.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('bms_transfer_items') && ! Schema::hasColumn('bms_transfer_items', 'asset_id')) {
            Schema::table('bms_transfer_items', function (Blueprint $t) {
                $t->unsignedBigInteger('asset_id')->nullable()->after('variation_id');
                $t->index(['asset_id']);
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('bms_transfer_items') && Schema::hasColumn('bms_transfer_items', 'asset_id')) {
            Schema::table('bms_transfer_items', function (Blueprint $t) {
                $t->dropIndex(['asset_id']);
                $t->dropColumn('asset_id');
            });
        }
    }
};
