<?php

namespace App\Provisioning\Steps\Host;

use App\Models\ProvisioningRun;
use App\Provisioning\StepResult;
use App\Services\Proxmox\ProxmoxClient;
use Throwable;

class VerifyProxmoxApi extends HostStep
{
    public function __construct(private ProxmoxClient $pve) {}

    public function key(): string
    {
        return 'verify_proxmox_api';
    }

    public function execute(ProvisioningRun $run): StepResult
    {
        $host = $this->host($run);

        try {
            $nodes = $this->pve->forHost($host)->listNodes();
        } catch (Throwable $e) {
            return StepResult::retry(15, 'Proxmox API not reachable: '.$e->getMessage());
        }

        if (empty($nodes)) {
            return StepResult::retry(15, 'Proxmox API returned no nodes yet');
        }

        return StepResult::advance();
    }
}
