<?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('grn_items', function (Blueprint $table) {
            // Batch tracking fields (when product tracking_mode includes 'batch').
            // batch_number already exists; rename semantically it is the supplier batch number.
            $table->date('expiry_date')->nullable()->after('batch_number');
            $table->date('manufacture_date')->nullable()->after('expiry_date');
            $table->decimal('batch_cost', 12, 2)->nullable()->after('manufacture_date');
            // Links back to the StockBatch created on GRN confirm.
            $table->unsignedBigInteger('stock_batch_id')->nullable()->after('batch_cost');

            // Serial tracking: JSON array of serial number strings entered on the GRN form.
            $table->json('serial_numbers')->nullable()->after('stock_batch_id');

            $table->index('stock_batch_id');
        });
    }

    public function down(): void
    {
        Schema::table('grn_items', function (Blueprint $table) {
            $table->dropIndex(['stock_batch_id']);
            $table->dropColumn(['expiry_date', 'manufacture_date', 'batch_cost', 'stock_batch_id', 'serial_numbers']);
        });
    }
};
