<?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
    {
        // Idempotency breadcrumbs: external IDs persisted before a step advances.
        // One resource per (run, kind) — enforces "created exactly once" on re-run.
        Schema::create('run_resources', function (Blueprint $table) {
            $table->id();
            $table->foreignId('run_id')->constrained('provisioning_runs')->cascadeOnDelete();
            $table->foreignId('host_id')->nullable()->constrained('hosts')->nullOnDelete();
            $table->string('kind');                       // wg_peer | pve_token | vmid | …
            $table->string('external_id');
            $table->timestamp('created_at')->nullable();

            $table->unique(['run_id', 'kind']);
        });
    }

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