<?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('collection_related_links', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->foreignId('related_collection_id')->constrained('collections')->cascadeOnDelete();
            $table->unsignedInteger('sort_order')->default(0);
            $table->foreignId('created_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['collection_id', 'related_collection_id'], 'collection_related_links_unique');
            $table->index(['collection_id', 'sort_order'], 'collection_related_links_sort_idx');
        });
    }

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