<?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('backup_schedule_runs', function (Blueprint $table) {
            $table->foreignId('user_id')
                ->nullable()
                ->after('managed_environment_id')
                ->constrained()
                ->nullOnDelete();

            $table->index(['user_id', 'created_at'], 'backup_schedule_runs_user_created');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('backup_schedule_runs', function (Blueprint $table) {
            $table->dropIndex('backup_schedule_runs_user_created');
            $table->dropConstrainedForeignId('user_id');
        });
    }
};
