<?php
// database/migrations/[timestamp]_add_indoregion_columns_to_tbl_users_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddIndoregionColumnsToTblUsersTable extends Migration
{
    public function up()
    {
        Schema::table('tbl_users', function (Blueprint $table) {
            // Tambah kolom kode wilayah
            $table->char('province_code', 2)->nullable()->after('address');
            $table->char('city_code', 4)->nullable()->after('province_code');
            $table->char('district_code', 7)->nullable()->after('city_code');
            $table->char('village_code', 10)->nullable()->after('district_code');
            $table->string('postal_code', 10)->nullable()->after('village_code');

            // Tambah foreign key
            $table->foreign('province_code')
                  ->references('code')
                  ->on(config('laravolt.indonesia.table_prefix').'provinces')
                  ->onDelete('set null');

            $table->foreign('city_code')
                  ->references('code')
                  ->on(config('laravolt.indonesia.table_prefix').'cities')
                  ->onDelete('set null');

            $table->foreign('district_code')
                  ->references('code')
                  ->on(config('laravolt.indonesia.table_prefix').'districts')
                  ->onDelete('set null');

            $table->foreign('village_code')
                  ->references('code')
                  ->on(config('laravolt.indonesia.table_prefix').'villages')
                  ->onDelete('set null');
        });
    }

    public function down()
    {
        Schema::table('tbl_users', function (Blueprint $table) {
            $table->dropForeign(['province_code']);
            $table->dropForeign(['city_code']);
            $table->dropForeign(['district_code']);
            $table->dropForeign(['village_code']);
            
            $table->dropColumn([
                'province_code',
                'city_code', 
                'district_code',
                'village_code',
                'postal_code'
            ]);
        });
    }
}