<?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('users', function (Blueprint $table) {
            if (!Schema::hasColumn('users', 'province_id')) $table->string('province_id')->nullable();
            if (!Schema::hasColumn('users', 'regency_id')) $table->string('regency_id')->nullable();
            if (!Schema::hasColumn('users', 'district_id')) $table->string('district_id')->nullable();
            if (!Schema::hasColumn('users', 'village_id')) $table->string('village_id')->nullable();
            if (!Schema::hasColumn('users', 'postal_code')) $table->string('postal_code')->nullable();
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $columns = ['province_id', 'regency_id', 'district_id', 'village_id', 'postal_code'];
            foreach ($columns as $col) {
                if (Schema::hasColumn('users', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};