<?php

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

/**
 * Consolidated legacy data-migration tracking tables (squashed 2026-07-11).
 *
 * These map IDs from a previous/external system onto the new primary keys.
 * Each table is created once with its final schema, guarded with hasTable
 * checks so installs that predate the squash don't collide on CREATE TABLE.
 *
 * Do not add future schema changes to this file — fresh installs and
 * existing installs would diverge. Create a new migration instead.
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $this->createIfMissing('legacy_pet_migrations', function (Blueprint $table) {
            $table->integer('old_pet_id')->primary();
            $table->foreignId('new_pet_id')->constrained('pets')->cascadeOnDelete();
            $table->timestamp('migrated_at');
        });

        $this->createIfMissing('legacy_adoption_migrations', function (Blueprint $table) {
            $table->integer('old_adoption_id')->primary();
            $table->foreignId('new_adoption_application_id')->constrained('adoption_applications')->cascadeOnDelete();
            $table->timestamp('migrated_at');
        });

        $this->createIfMissing('legacy_user_migrations', function (Blueprint $table) {
            $table->integer('old_user_id')->primary();
            $table->foreignId('new_user_id')->constrained('users')->cascadeOnDelete();
            $table->timestamp('migrated_at');
        });

        $this->createIfMissing('legacy_fostering_migrations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('old_fostering_id')->unique();
            $table->unsignedBigInteger('new_fostering_application_id');
            $table->timestamp('migrated_at');

            $table->foreign('new_fostering_application_id', 'legacy_fostering_fk')
                ->references('id')
                ->on('fostering_applications')
                ->cascadeOnDelete();
        });

        $this->createIfMissing('legacy_assistance_migrations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('old_assistance_id')->unique();
            $table->unsignedBigInteger('new_assistance_application_id');
            $table->timestamp('migrated_at');

            $table->foreign('new_assistance_application_id', 'legacy_assistance_fk')
                ->references('id')
                ->on('assistance_applications')
                ->cascadeOnDelete();
        });

        $this->createIfMissing('legacy_form_question_migrations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('old_question_id')->unique();
            $table->unsignedBigInteger('new_question_id');
            $table->timestamp('migrated_at');

            $table->index('old_question_id');
            $table->index('new_question_id');
        });

        $this->createIfMissing('legacy_form_question_group_migrations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('old_group_id')->unique();
            $table->unsignedBigInteger('new_group_id');
            $table->timestamp('migrated_at');

            $table->index('old_group_id');
            $table->index('new_group_id');
        });

        $this->createIfMissing('legacy_page_migrations', function (Blueprint $table) {
            $table->integer('old_page_id')->primary();
            $table->foreignId('new_page_id')->constrained('pages')->cascadeOnDelete();
            $table->timestamp('migrated_at');
        });

        $this->createIfMissing('legacy_blog_post_migrations', function (Blueprint $table) {
            $table->integer('old_blog_id')->primary();
            $table->foreignId('new_blog_post_id')->constrained('blog_posts')->cascadeOnDelete();
            $table->timestamp('migrated_at');
        });
    }

    /**
     * Rollback is intentionally blocked.
     *
     * Squashed migrations consolidate the entire schema into one pass and have
     * no meaningful prior state to restore. Rolling back would drop live data
     * on existing installs, so rollback is intentionally blocked. Use
     * `php artisan migrate:fresh` to rebuild the schema from scratch on a
     * non-production database.
     */
    public function down(): void
    {
        throw new RuntimeException(
            'Squashed migrations cannot be rolled back. Run `php artisan migrate:fresh` to rebuild the schema on a non-production database.'
        );
    }

    private function createIfMissing(string $tableName, Closure $callback): void
    {
        if (! Schema::hasTable($tableName)) {
            Schema::create($tableName, $callback);
        }
    }
};
