<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateChaptersTable extends Migration
{
    public function up(): void
    {
        Schema::create('chapters', function (Blueprint $table) {
            $table->id();
            $table->string('hashkey')->unique();
            $table->string('name');
            $table->enum('level', ['national', 'region', 'province', 'city', 'barangay']);
            $table->unsignedBigInteger('parent_id')->nullable();
            $table->string('location_key')->nullable()->comment('Normalized slug for auto-matching UserInfo address fields');
            $table->decimal('lat', 10, 7)->nullable();
            $table->decimal('lng', 10, 7)->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->foreign('parent_id')->references('id')->on('chapters')->nullOnDelete();
            $table->index(['level', 'is_active']);
            $table->index('location_key');
        });
    }

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