<?php

namespace App\Provisioning\Steps;

use App\Models\Host;
use App\Models\ProvisioningRun;
use App\Models\RunResource;

/**
 * Idempotency breadcrumbs shared by host and customer steps: persist an external
 * resource id (kind unique per run) BEFORE the creating step advances.
 */
trait ManagesRunResources
{
    protected function recordResource(ProvisioningRun $run, ?Host $host, string $kind, string $externalId): void
    {
        RunResource::firstOrCreate(
            ['run_id' => $run->id, 'kind' => $kind],
            ['host_id' => $host?->id, 'external_id' => $externalId],
        );
    }

    protected function hasResource(ProvisioningRun $run, string $kind): bool
    {
        return RunResource::query()->where('run_id', $run->id)->where('kind', $kind)->exists();
    }
}
