<?php

namespace App\Support;

/**
 * Splits what Proxmox calls itself into the part a human reads and the part
 * almost nobody does.
 *
 * `pve-manager/9.2.6/7f8d010005bd7231` shown raw is worse than useless in a
 * narrow card: the string is truncated somewhere inside the build hash, so the
 * console displayed everything EXCEPT the version number — the only part of it
 * anyone looks for.
 */
final class PveVersion
{
    /**
     * @return array{version: ?string, build: ?string}
     */
    public static function parse(?string $raw): array
    {
        $raw = trim((string) $raw);

        if ($raw === '') {
            return ['version' => null, 'build' => null];
        }

        // pve-manager/<version>/<build>. The build is optional — some hosts
        // report only the first two segments.
        if (preg_match('#^[a-z-]+/([0-9][0-9.]*)(?:/([0-9a-f]+))?$#i', $raw, $m) === 1) {
            return ['version' => $m[1], 'build' => $m[2] ?? null];
        }

        // An unexpected shape is passed through whole rather than swallowed. A
        // display that shows nothing when it does not recognise something is
        // worse than one that hands over what it was given: the operator can
        // still read it, and nobody is left wondering whether the field is
        // empty or the parser gave up.
        return ['version' => $raw, 'build' => null];
    }
}
