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

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

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