<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addresses', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuidMorphs('addressable');
            $table->string('street');
            $table->string('unit')->nullable();
            $table->string('city');
            $table->string('state');
            $table->string('postal_code'); // leave as string to accomodate 12345-6789 or Canada
            $table->string('country')->default('United States');
            $table->float('latitude', 12, 9)->nullable();
            $table->float('longitude', 12, 9)->nullable();
            $table->point('location')->nullable();
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();

            $table->index(['latitude', 'longitude']);
            $table->spatialIndex('location');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('addresses');
    }
}
