<?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::table('alert_deliveries', function (Blueprint $table): void {
            $table->unsignedBigInteger('managed_environment_id')->nullable()->change();
            $table->unsignedBigInteger('alert_rule_id')->nullable()->change();

            $table->index(
                ['workspace_id', 'alert_destination_id', 'event_type', 'created_at'],
                'alert_deliveries_last_test_lookup_index',
            );
        });
    }

    public function down(): void
    {
        Schema::table('alert_deliveries', function (Blueprint $table): void {
            $table->dropIndex('alert_deliveries_last_test_lookup_index');

            $table->unsignedBigInteger('managed_environment_id')->nullable(false)->change();
            $table->unsignedBigInteger('alert_rule_id')->nullable(false)->change();
        });
    }
};
