<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('findings', function (Blueprint $table): void {
            $table->string('recurrence_key', 64)->nullable();

            $table->index(['managed_environment_id', 'status', 'due_at'], 'findings_tenant_status_due_at_idx');
            $table->index(['managed_environment_id', 'assignee_user_id'], 'findings_tenant_assignee_idx');
            $table->index(['managed_environment_id', 'recurrence_key'], 'findings_tenant_recurrence_key_idx');
            $table->index(['workspace_id', 'status', 'due_at'], 'findings_workspace_status_due_at_idx');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('findings', function (Blueprint $table): void {
            $table->dropIndex('findings_workspace_status_due_at_idx');
            $table->dropIndex('findings_tenant_recurrence_key_idx');
            $table->dropIndex('findings_tenant_assignee_idx');
            $table->dropIndex('findings_tenant_status_due_at_idx');

            $table->dropColumn('recurrence_key');
        });
    }
};
