// pure decision: should the laptop installer replace snap-confined
// Chromium with Google Chrome stable, and which absolute binary path should
// vnc.sh / writeChromiumWrapper / the Playwright env var resolve to?
//
// Mirrors apt-resolve.ts and port-resolution.ts: inputs
// in, decision out, no I/O. The installer wraps this with the actual
// `command -v` and `realpath` spawnSync calls and the side-effecting
// apt-repo-add → apt-get install → writeFileSync sequence.
//
// Snap detection: on Ubuntu Noble (the only currently-supported snap-confined
// distro for Maxy/Real Agent) `readlink -f /usr/bin/chromium` resolves to
// `/usr/bin/snap` — the snap launcher binary, which then re-execs into
// `/snap/chromium/<rev>/usr/lib/chromium/chromium` under AppArmor. The
// AppArmor `home` interface profile excludes hidden top-level paths under
// `$HOME`, so any write to `~/.maxy/chromium-profile/SingletonLock` (and the
// equivalent realagent path) hits EACCES regardless of UID. The fix is binary
// replacement at install time; runtime patches are forbidden by
// `feedback_no_admin_upgrade_path.md`.
/**
 * True iff the realpath resolves to the snap launcher (`/usr/bin/snap`) or
 * lives anywhere under `/snap/`. Splitting on `/` and checking for a literal
 * `snap` segment catches all three real-world shapes:
 *   - `/snap/bin/chromium`               (intermediate snap wrapper symlink)
 *   - `/snap/chromium/2924/usr/...`      (squashfs-mounted snap revision)
 *   - `/usr/bin/snap`                    (snap launcher — `readlink -f` terminus on Noble)
 *
 * Empty / null inputs return false so callers can pass through `realpathSync`
 * results without an explicit guard.
 */
export function isSnapConfinedPath(realpath) {
    if (!realpath)
        return false;
    return realpath.split("/").includes("snap");
}
/**
 * Pure decision. Resolution order:
 *   1. Non-linux platform → skip-non-linux. Darwin Maxy uses Playwright's
 *      managed Chromium (brew-install.ts), Pi-only chromium-binary.path
 *      logic does not apply. No file is written.
 *   2. A non-snap `google-chrome-stable` is on PATH → use it. Covers re-run
 *      installs on a previously-fixed laptop and operator-supplied Chrome.
 *   3. A non-snap `chromium` is on PATH → use it. Covers Pi Bookworm where
 *      `apt-get install chromium` lands a real .deb, not a snap symlink.
 *   4. `chromium` is on PATH but realpath is snap-confined, and no
 *      google-chrome-stable is present → install-google-chrome. The caller
 *      adds Google's signed apt repo + installs google-chrome-stable, then
 *      writes the resolved path.
 *   5. Neither chromium nor google-chrome-stable is on PATH → fail. The VNC
 *      apt step ran before this decision, so chromium absence here means
 *      `apt-get install chromium` did not land its binary — a contract
 *      breach the installer must surface loudly.
 */
export function decideChromiumAction(input) {
    const { platform, whichChromium, realpathChromium, whichGoogleChrome, realpathGoogleChrome } = input;
    if (platform !== "linux") {
        return {
            action: "skip-non-linux",
            resolvedPath: null,
            reason: `platform=${platform} — chromium-binary.path is linux-only`,
        };
    }
    if (whichGoogleChrome && !isSnapConfinedPath(realpathGoogleChrome)) {
        return {
            action: "use",
            resolvedPath: whichGoogleChrome,
            reason: `google-chrome-stable already installed (realpath=${realpathGoogleChrome})`,
        };
    }
    if (whichChromium && !isSnapConfinedPath(realpathChromium)) {
        return {
            action: "use",
            resolvedPath: whichChromium,
            reason: `chromium is non-snap (realpath=${realpathChromium})`,
        };
    }
    if (whichChromium && isSnapConfinedPath(realpathChromium)) {
        return {
            action: "install-google-chrome",
            resolvedPath: null,
            reason: `chromium is snap-confined (realpath=${realpathChromium}) — installing google-chrome-stable`,
        };
    }
    return {
        action: "fail",
        resolvedPath: null,
        reason: "neither chromium nor google-chrome-stable is on PATH after VNC apt install — contract breach",
    };
}
