<?php

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

return new class extends Migration
{
    /**
     * Add block registry reference columns to the existing page_list table.
     */
    public function up(): void
    {
        if (!Schema::hasColumn('page_list', 'block_id')) {
            Schema::table('page_list', function (Blueprint $table) {
                $table->unsignedInteger('block_id')->nullable()->after('id');
                $table->boolean('is_from_registry')->default(false)->after('block_id');
                $table->string('structure_hash', 64)->nullable()->after('is_from_registry');

                $table->index('block_id');
                $table->foreign('block_id')->references('id')->on('block_registry')->onDelete('set null');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (Schema::hasColumn('page_list', 'block_id')) {
            Schema::table('page_list', function (Blueprint $table) {
                $table->dropForeign(['block_id']);
                $table->dropIndex(['block_id']);
                $table->dropColumn(['block_id', 'is_from_registry', 'structure_hash']);
            });
        }
    }
};
