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

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

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