<?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
    {
        if (! Schema::hasTable('inventory_items') || Schema::hasColumn('inventory_items', 'workspace_id')) {
            return;
        }

        Schema::table('inventory_items', function (Blueprint $table): void {
            $table->unsignedBigInteger('workspace_id')->nullable();
            $table->index('workspace_id');
            $table->index(['workspace_id', 'managed_environment_id']);
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('inventory_items') || ! Schema::hasColumn('inventory_items', 'workspace_id')) {
            return;
        }

        Schema::table('inventory_items', function (Blueprint $table): void {
            $table->dropIndex(['workspace_id', 'managed_environment_id']);
            $table->dropIndex(['workspace_id']);
            $table->dropColumn('workspace_id');
        });
    }
};
