<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('grn_items', function (Blueprint $table) {
            // Add IGP item relationship
            $table->foreignId('igp_item_id')->nullable()->after('purchase_order_item_id')->constrained('inward_gate_pass_items')->nullOnDelete();

            // Add validation fields
            $table->decimal('po_qty', 18, 6)->nullable()->after('rejected_quantity');
            $table->decimal('igp_qty', 18, 6)->nullable()->after('po_qty');

            // Add indexes
            $table->index(['igp_item_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('grn_items', function (Blueprint $table) {
            $table->dropIndex(['igp_item_id']);
            $table->dropConstrainedForeignId('igp_item_id');
            $table->dropColumn(['po_qty', 'igp_qty']);
        });
    }
};
