<?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('archive_runs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('archive_setting_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('status', 30)->default('running'); // running|success|error
            $table->json('counts')->nullable(); // per-table affected counts
            $table->json('context')->nullable(); // manual context scope
            $table->timestamp('started_at')->nullable();
            $table->timestamp('finished_at')->nullable();
            $table->unsignedInteger('duration_ms')->nullable();
            $table->text('message')->nullable();
            $table->timestamps();
            $table->index(['archive_setting_id', 'status']);
        });
    }

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