<?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::table('park_areas', function (Blueprint $table) {
            // Add position field for ordering
            $table->integer('position')->default(0);
            // Add parent_id for nested areas
            $table->foreignId('parent_id')
                ->nullable()
                ->constrained('park_areas')
                ->nullOnDelete();
            
            // Add index for efficient ordering queries
            $table->index(['park_id', 'position']);
            // Add index for parent relationship queries
            $table->index(['park_id', 'parent_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('park_areas', function (Blueprint $table) {
            $table->dropIndex(['park_id', 'position']);
            $table->dropIndex(['park_id', 'parent_id']);
            $table->dropForeign(['parent_id']);
            $table->dropColumn(['position', 'parent_id']);
        });
    }
};