<?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('inventory_orders', function (Blueprint $table) {
            if (!Schema::hasColumn('inventory_orders', 'purchase_quantity')) {
                $table->decimal('purchase_quantity', 10, 3)->nullable()->after('quantity');
            }
            if (!Schema::hasColumn('inventory_orders', 'purchase_unit')) {
                $table->string('purchase_unit')->nullable()->after('purchase_quantity');
            }
            if (!Schema::hasColumn('inventory_orders', 'stock_unit')) {
                $table->string('stock_unit')->nullable()->after('purchase_unit');
            }
            if (!Schema::hasColumn('inventory_orders', 'unit_conversion_factor')) {
                $table->decimal('unit_conversion_factor', 10, 3)->nullable()->after('stock_unit');
            }
        });
    }

    public function down(): void
    {
        Schema::table('inventory_orders', function (Blueprint $table) {
            foreach (['unit_conversion_factor', 'stock_unit', 'purchase_unit', 'purchase_quantity'] as $column) {
                if (Schema::hasColumn('inventory_orders', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
