<?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::create('user_alamat', function (Blueprint $table) {
    $table->bigIncrements('ID');

    $table->unsignedBigInteger('user_id');

    // 🔥 relasi laravolt (pakai CODE)
    $table->char('province_code', 2);
    $table->char('city_code', 4);
    $table->char('district_code', 7);
    $table->char('village_code', 10);

    $table->string('nama_penerima');
    $table->string('no_hp');
    $table->text('alamat_lengkap');
    $table->string('kode_pos')->nullable();

    $table->decimal('latitude', 10, 7)->nullable();
    $table->decimal('longitude', 10, 7)->nullable();

    $table->boolean('alamat_utama')->default(false);
    $table->timestamps();

    // FK user
    $table->foreign('user_id')
        ->references('ID')
        ->on('users')
        ->onDelete('cascade');

    // 🔥 FK laravolt
    $table->foreign('province_code')->references('code')->on('indonesia_provinces');
    $table->foreign('city_code')->references('code')->on('indonesia_cities');
    $table->foreign('district_code')->references('code')->on('indonesia_districts');
    $table->foreign('village_code')->references('code')->on('indonesia_villages');
});
    }

    public function down(): void
    {
        Schema::dropIfExists('user_alamat');
    }
};
