<?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::create('restore_runs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();
            $table->foreignId('backup_set_id')->constrained()->cascadeOnDelete();
            $table->string('requested_by')->nullable();
            $table->boolean('is_dry_run')->default(true);
            $table->string('status')->default('pending');
            $table->json('requested_items')->nullable();
            $table->json('preview')->nullable();
            $table->json('results')->nullable();
            $table->text('failure_reason')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->index(['managed_environment_id', 'status']);
            $table->index('started_at');
            $table->index('completed_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('restore_runs');
    }
};
