<?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::create('backup_schedule_runs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('backup_schedule_id')->constrained('backup_schedules')->cascadeOnDelete();
            $table->foreignId('managed_environment_id')->constrained('managed_environments')->cascadeOnDelete();
            $table->dateTime('scheduled_for');
            $table->dateTime('started_at')->nullable();
            $table->dateTime('finished_at')->nullable();
            $table->enum('status', ['running', 'success', 'partial', 'failed', 'canceled', 'skipped']);
            $table->json('summary')->nullable();
            $table->string('error_code')->nullable();
            $table->text('error_message')->nullable();
            $table->foreignId('backup_set_id')->nullable()->constrained('backup_sets')->nullOnDelete();
            $table->timestamps();

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('backup_schedule_runs');
    }
};
