<?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
    {
        // Check if table exists
        if (Schema::hasTable('t_stock_opname_change_logs')) {
            Schema::table('t_stock_opname_change_logs', function (Blueprint $table) {
                // Check if column exists before dropping
                if (Schema::hasColumn('t_stock_opname_change_logs', 'change_reason')) {
                    $table->dropColumn('change_reason');
                }
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Check if table exists
        if (Schema::hasTable('t_stock_opname_change_logs')) {
            Schema::table('t_stock_opname_change_logs', function (Blueprint $table) {
                // Re-add column if needed for rollback
                if (!Schema::hasColumn('t_stock_opname_change_logs', 'change_reason')) {
                    $table->text('change_reason')->nullable()->after('new_value');
                }
            });
        }
    }
};
