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

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

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

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