<?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('good_received_notes', function (Blueprint $table) {
            // Add missing fields
            $table->foreignId('received_by_id')->nullable()->after('purchase_order_id')->constrained('users')->nullOnDelete();
            $table->string('vehicle_no')->nullable()->after('received_date');
            $table->foreignId('gate_pass_id')->nullable()->after('vehicle_no')->constrained('gate_passes')->nullOnDelete();

            // Update status enum to include additional states
            $table->enum('status', ['pending_qc', 'accepted', 'rejected', 'putaway_pending', 'posted', 'partial'])->change();

            // Add indexes for better performance
            $table->index(['received_by_id']);
            $table->index(['gate_pass_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('good_received_notes', function (Blueprint $table) {
            $table->dropIndex(['received_by_id']);
            $table->dropIndex(['gate_pass_id']);

            $table->dropConstrainedForeignId('received_by_id');
            $table->dropConstrainedForeignId('gate_pass_id');
            $table->dropColumn(['vehicle_no']);

            // Revert status enum
            $table->enum('status', ['pending_qc', 'accepted', 'rejected', 'partial'])->change();
        });
    }
};
