<?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('addresses', function (Blueprint $table) {
            $table->id();
            $table->string('source');
            $table->unsignedBigInteger('source_id');
            $table->string('zip_code');
            $table->text('address');
            $table->boolean('has_complement')->default(false);
            $table->string('complement')->nullable();
            $table->string('nickname')->nullable();
            $table->text('instructions')->nullable();
            $table->unsignedBigInteger('city_id')->nullable();
            $table->foreign('city_id')->references('id')->on('cities')->onDelete('set null');
            $table->unsignedBigInteger('state_id')->nullable();
            $table->foreign('state_id')->references('id')->on('states')->onDelete('set null');
            $table->enum('address_type', ['home', 'commercial', 'other']);
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index(['source', 'source_id']);
            $table->index('zip_code');
        });
    }

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