<?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(): void
    {
        Schema::create('locations', function (Blueprint $table) {
            $table->id();
            $table->string('type', 24);
            $table->string('name');
            $table->char('country_code', 2)->nullable();
            $table->char('iso_code', 2)->nullable()->unique();
            $table->foreignId('parent_id')->nullable()->constrained('locations');
            $table->string('source', 32);
            $table->string('source_key', 128)->nullable();
            $table->timestamps();

            $table->index(['type', 'country_code', 'name']);
            $table->unique(['type', 'source', 'source_key']);
        });
    }

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