// ─────────────────────────────────────────────────────────────────────────────
// Nexus Prime — Install Registry
//
// Registers npm installs with Supabase for tracking. Fire-and-forget pattern
// (same as telemetry-remote.ts line 450). Uses direct Supabase REST POST
// with upsert to handle repeated installs gracefully.
// ─────────────────────────────────────────────────────────────────────────────
const REGISTRY_ENDPOINT = process.env.NEXUS_REGISTRY_ENDPOINT ?? '';
const REGISTRY_KEY = process.env.NEXUS_REGISTRY_KEY ?? '';
/**
 * Register an npm install with the Supabase installs table.
 * Fire-and-forget — never blocks, never throws.
 */
export function registerInstall(installId, version) {
    if (!REGISTRY_ENDPOINT || !REGISTRY_KEY)
        return;
    const payload = {
        install_id: installId,
        nexus_version: version,
        os: process.platform,
        arch: process.arch,
        node_version: process.version,
        is_ci: process.env.CI === 'true',
        last_seen_at: new Date().toISOString(),
    };
    fetch(REGISTRY_ENDPOINT, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            apikey: REGISTRY_KEY,
            Authorization: `Bearer ${REGISTRY_KEY}`,
            Prefer: 'return=minimal,resolution=merge-duplicates',
        },
        body: JSON.stringify(payload),
        signal: AbortSignal.timeout(5_000),
    }).catch(() => {
        // Swallow — telemetry is best-effort, never blocks
    });
}
/**
 * Link an install to a user after license activation.
 * Fire-and-forget.
 */
export function linkInstallToUser(installId, userId) {
    if (!REGISTRY_ENDPOINT || !REGISTRY_KEY)
        return;
    fetch(REGISTRY_ENDPOINT, {
        method: 'PATCH',
        headers: {
            'Content-Type': 'application/json',
            apikey: REGISTRY_KEY,
            Authorization: `Bearer ${REGISTRY_KEY}`,
            Prefer: 'return=minimal',
        },
        body: JSON.stringify({
            install_id: installId,
            user_id: userId,
            last_seen_at: new Date().toISOString(),
        }),
        signal: AbortSignal.timeout(5_000),
    }).catch(() => {
        // Swallow
    });
}
