<?php

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

return new class extends Migration
{
    /**
     * Block Registry core tables — categories, blocks, fields, template
     * assignments, page_list links, and version history.
     */
    public function up(): void
    {
        // Block categories for organization
        Schema::create('block_registry_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 100);
            $table->string('slug', 100)->unique();
            $table->string('icon', 50)->nullable();
            $table->integer('order_index')->default(0);
            $table->unsignedInteger('parent_id')->nullable();
            $table->timestamps();

            $table->index('parent_id');
            $table->index('order_index');
        });

        // Core block definitions
        Schema::create('block_registry', function (Blueprint $table) {
            $table->increments('id');
            $table->char('uuid', 36)->unique();
            $table->string('name', 255);
            $table->string('slug', 255)->unique();
            $table->text('description')->nullable();
            $table->unsignedInteger('category_id')->nullable();
            $table->string('icon', 100)->nullable();
            $table->string('preview_image', 255)->nullable();
            $table->string('template', 255)->nullable();
            $table->boolean('is_system')->default(false);
            $table->boolean('is_active')->default(true);
            $table->integer('version')->default(1);
            $table->integer('order_index')->default(0);
            $table->timestamps();

            $table->index('category_id');
            $table->index('slug');
            $table->index('is_system');
            $table->index('is_active');
            $table->index('order_index');

            $table->foreign('category_id')
                ->references('id')
                ->on('block_registry_categories')
                ->onDelete('set null');
        });

        // Block field definitions (structure)
        Schema::create('block_registry_fields', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('block_id');
            $table->unsignedInteger('parent_field_id')->nullable();
            $table->string('name', 64);
            $table->string('label', 255)->nullable();
            $table->string('type', 32)->default('text');
            $table->integer('order_index')->default(0);

            // Field configuration
            $table->boolean('required')->default(false);
            $table->string('css_class', 125)->nullable();
            $table->text('settings')->nullable();   // JSON: editor config, image sizes, etc.
            $table->text('options')->nullable();     // JSON: select options, etc.
            $table->text('regex')->nullable();
            $table->string('mime', 255)->nullable();
            $table->text('default_value')->nullable();
            $table->text('info')->nullable();        // Help text

            // Repeater support
            $table->boolean('is_repeater')->default(false);
            $table->integer('repeater_min')->default(1);
            $table->integer('repeater_max')->default(200);
            $table->string('repeater_text', 64)->default('Add another');

            $table->timestamps();

            $table->index(['block_id', 'order_index']);
            $table->index('parent_field_id', 'idx_parent_field');

            $table->foreign('block_id')
                ->references('id')
                ->on('block_registry')
                ->onDelete('cascade');

            $table->foreign('parent_field_id')
                ->references('id')
                ->on('block_registry_fields')
                ->onDelete('cascade');
        });

        // Template whitelist (which blocks can be added to which templates)
        Schema::create('block_registry_template_assignments', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('block_id');
            $table->string('template_name', 64);
            $table->boolean('is_default')->default(false);
            $table->integer('order_index')->default(0);
            $table->timestamps();

            $table->unique(['block_id', 'template_name'], 'unique_block_template');
            $table->index('template_name');

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

        // Track which page_lists use which blocks (for live sync)
        Schema::create('page_list_block_links', function (Blueprint $table) {
            $table->integer('page_list_id'); // Matches page_list.id (signed int)
            $table->unsignedInteger('block_id');
            $table->string('structure_hash', 64)->nullable();
            $table->timestamp('synced_at')->useCurrent();

            $table->primary(['page_list_id', 'block_id']);
            $table->index('block_id');

            $table->foreign('page_list_id')
                ->references('id')
                ->on('page_list')
                ->onDelete('cascade');

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

        // Audit log for block changes (for rollback/history)
        Schema::create('block_registry_versions', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('block_id');
            $table->integer('version');
            $table->json('structure');
            $table->unsignedInteger('changed_by')->nullable();
            $table->string('change_description', 255)->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['block_id', 'version']);

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('block_registry_versions');
        Schema::dropIfExists('page_list_block_links');
        Schema::dropIfExists('block_registry_template_assignments');
        Schema::dropIfExists('block_registry_fields');
        Schema::dropIfExists('block_registry');
        Schema::dropIfExists('block_registry_categories');
    }
};
