<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('page_translations', function (Blueprint $table) {
            $table->string('nested_slug')->nullable()->after('slug');
            $table->index('nested_slug');
        });

        // Populate nested_slug for existing records (flat pages: nested_slug = slug)
        DB::table('page_translations')
            ->whereNotNull('slug')
            ->update(['nested_slug' => DB::raw('slug')]);
    }

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