<?php

namespace App\Provisioning\Jobs;

use App\Models\ProvisioningRun;
use App\Provisioning\RunRunner;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class AdvanceRunJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /** The run state machine owns retries — the job itself runs once. */
    public $tries = 1;

    /** Above the longest step (InstallProxmoxVe: 1800s) so the worker doesn't kill it. */
    public $timeout = 2100;

    public function __construct(public string $runUuid)
    {
        $this->onConnection('provisioning');
        $this->onQueue('provisioning');
    }

    public function handle(RunRunner $runner): void
    {
        $run = ProvisioningRun::query()->where('uuid', $this->runUuid)->first();

        if ($run !== null) {
            $runner->advance($run);
        }
    }
}
