<?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
    {
        // WAJIB: Cek tabel ada atau tidak sebelum modifikasi
        if (Schema::hasTable('users')) {
            // WAJIB: Cek kolom sebelum menambah
            if (!Schema::hasColumn('users', 'flag_backdate_so')) {
                Schema::table('users', function (Blueprint $table) {
                    $table->boolean('flag_backdate_so')
                        ->default(false)
                        ->after('email')
                        ->comment('Flag untuk izin backdate Stock Opname');
                });
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // WAJIB: Implementasi rollback yang aman
        if (Schema::hasTable('users')) {
            if (Schema::hasColumn('users', 'flag_backdate_so')) {
                Schema::table('users', function (Blueprint $table) {
                    $table->dropColumn('flag_backdate_so');
                });
            }
        }
    }
};
