<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('user_merge_links', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('source_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('target_user_id')->constrained('users')->cascadeOnDelete();
            $table->enum('entity_type', ['order', 'withdrawal'])->index();
            $table->unsignedBigInteger('entity_id');
            $table->timestamps();
            $table->unique(['source_user_id', 'entity_type', 'entity_id'], 'user_merge_links_unique');
            $table->index(['target_user_id', 'entity_type']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('user_merge_links');
    }
};
