<?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('stored_reports', function (Blueprint $table) {
            $table->string('fingerprint', 64)->nullable()->after('payload');
            $table->string('previous_fingerprint', 64)->nullable()->after('fingerprint');

            $table->unique(['managed_environment_id', 'report_type', 'fingerprint']);
            $table->index(['managed_environment_id', 'report_type', 'created_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('stored_reports', function (Blueprint $table) {
            $table->dropIndex(['managed_environment_id', 'report_type', 'created_at']);
            $table->dropUnique(['managed_environment_id', 'report_type', 'fingerprint']);
            $table->dropColumn(['fingerprint', 'previous_fingerprint']);
        });
    }
};
