<?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('inventory_links', function (Blueprint $table) {
            $table->id();
            $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();
            $table->string('source_type');
            $table->uuid('source_id');
            $table->string('target_type');
            $table->uuid('target_id')->nullable();
            $table->string('relationship_type');
            $table->jsonb('metadata')->nullable();
            $table->timestamps();

            $table->unique([
                'managed_environment_id',
                'source_type',
                'source_id',
                'target_type',
                'target_id',
                'relationship_type',
            ], 'inventory_links_unique');

            $table->index(['managed_environment_id', 'source_type', 'source_id']);
            $table->index(['managed_environment_id', 'target_type', 'target_id']);
        });
    }

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