<?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('order_items', function (Blueprint $table) {
            // Cek apakah kolom sudah ada sebelum menambahkan
            if (!Schema::hasColumn('order_items', 'qty_satuan')) {
                $table->integer('qty_satuan')->nullable()->after('qty');
            }
            if (!Schema::hasColumn('order_items', 'harga_satuan')) {
                $table->decimal('harga_satuan', 15, 2)->nullable()->after('price');
            }
            if (!Schema::hasColumn('order_items', 'unit_type')) {
                $table->string('unit_type')->default('bulk')->after('harga_satuan');
            }
            if (!Schema::hasColumn('order_items', 'unit_name')) {
                $table->string('unit_name')->nullable()->after('unit_type');
            }
        });
    }

    public function down(): void
    {
        Schema::table('order_items', function (Blueprint $table) {
            $columns = ['qty_satuan', 'harga_satuan', 'unit_type', 'unit_name'];
            foreach ($columns as $column) {
                if (Schema::hasColumn('order_items', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
