<?php

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

return new class extends Migration
{
    /**
     * Self-referential rotation twin for every preset row. The Phase 2
     * resolver swaps `devicePresetId` on frame layers when a portrait master
     * resolves into a landscape factor — that swap needs an O(1) lookup, not a
     * key-suffix string match at render time. nullOnDelete keeps a preset
     * deactivation from cascade-breaking its pair (the deprecated row is
     * filtered by `is_active` anyway).
     */
    public function up(): void
    {
        Schema::table('device_presets', function (Blueprint $table) {
            $table->unsignedBigInteger('rotation_pair_id')->nullable()->after('form_factor');
            $table->foreign('rotation_pair_id')
                ->references('id')
                ->on('device_presets')
                ->nullOnDelete();
            $table->index('rotation_pair_id');
        });
    }

    public function down(): void
    {
        Schema::table('device_presets', function (Blueprint $table) {
            $table->dropForeign(['rotation_pair_id']);
            $table->dropIndex(['rotation_pair_id']);
            $table->dropColumn('rotation_pair_id');
        });
    }
};
