<?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()
    {
        Schema::table('users', function (Blueprint $table) {
            // Cek apakah kolom sudah ada sebelum menambahkannya
            if (! Schema::hasColumn('users', 'phone')) {
                $table->string('phone')->nullable();
            }
            if (! Schema::hasColumn('users', 'province_id')) {
                $table->string('province_id')->nullable();
            }
            if (! Schema::hasColumn('users', 'province_name')) {
                $table->string('province_name')->nullable();
            }

            // UBAH: Sesuaikan dengan Model & Controller (city_id & city_name)
            if (! Schema::hasColumn('users', 'city_id')) {
                $table->string('city_id')->nullable();
            }
            if (! Schema::hasColumn('users', 'city_name')) {
                $table->string('city_name')->nullable();
            }

            // UBAH: Sesuaikan dengan Model (detail_address)
            if (! Schema::hasColumn('users', 'detail_address')) {
                $table->text('detail_address')->nullable();
            }

            // Hapus district_id dan village_id jika memang tidak dipakai di Model/Controller
            // Tapi jika kamu mau menyimpannya untuk ke depannya, uncomment di bawah ini:
            /*
            if (!Schema::hasColumn('users', 'district_id')) { $table->string('district_id')->nullable(); }
            if (!Schema::hasColumn('users', 'district_name')) { $table->string('district_name')->nullable(); }
            if (!Schema::hasColumn('users', 'village_id')) { $table->string('village_id')->nullable(); }
            if (!Schema::hasColumn('users', 'village_name')) { $table->string('village_name')->nullable(); }
            */
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn([
                'phone', 'province_id', 'province_name',
                'city_id', 'city_name', 'detail_address',
            ]);
        });
    }
};
