<?php

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

return new class extends Migration
{
    /**
     * Add a region column so an entity can host blocks in multiple named
     * areas (e.g. a "header" region above a sidebar/main split). Existing
     * rows stay NULL and are treated as the default "main" region.
     */
    public function up(): void
    {
        if (!Schema::hasTable('entity_blocks')) {
            return;
        }

        if (Schema::hasColumn('entity_blocks', 'region')) {
            return;
        }

        Schema::table('entity_blocks', function (Blueprint $table) {
            $table->string('region', 64)->nullable()->after('order');
            $table->index(['entity_type', 'entity_id', 'region'], 'entity_blocks_entity_region_index');
        });
    }

    public function down(): void
    {
        if (!Schema::hasColumn('entity_blocks', 'region')) {
            return;
        }

        Schema::table('entity_blocks', function (Blueprint $table) {
            $table->dropIndex('entity_blocks_entity_region_index');
            $table->dropColumn('region');
        });
    }
};
