/**
 * One-time "star us on GitHub" nudge.
 *
 * Printed a single time, ever, after a user's first *successful* headline run
 * (`agents run` / `agents teams`). Modelled on the existing warn-once sentinel
 * pattern (see maybeWarnMultiInstall in src/index.ts): a marker under the
 * regenerable runtime-state dir records that the hint was shown so it never
 * repeats. It is a plain inline line — not a toast, not a nag — and stays out
 * of the way of non-interactive, CI, quiet, and JSON output.
 */
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
import { getRuntimeStateDir } from './state.js';
/** Canonical GitHub repo the nudge points at (renamed from agents-cli). */
export const REPO_URL = 'https://github.com/phnx-labs/agi-cli';
/** Sentinel written the first (and only) time the nudge is shown. */
function nudgeSentinelPath() {
    return path.join(getRuntimeStateDir(), 'star-nudge-shown');
}
/** Has the star nudge already been shown on this machine? */
export function hasShownStarNudge() {
    try {
        return fs.existsSync(nudgeSentinelPath());
    }
    catch {
        return false;
    }
}
/**
 * Pure decision: should the one-time star nudge be shown? Skipped for
 * quiet/JSON output, non-interactive terminals (pipes, redirects), CI, an
 * explicit opt-out, or once it has already been shown.
 */
export function shouldShowStarNudge(ctx) {
    if (ctx.quiet)
        return false;
    if (!ctx.isTTY)
        return false;
    if (ctx.ci)
        return false;
    if (ctx.optedOut)
        return false;
    if (ctx.alreadyShown)
        return false;
    return true;
}
/**
 * Show the one-time star nudge if it hasn't been shown yet and the context is
 * appropriate. Best-effort by contract: never throws, never blocks the run it
 * follows.
 *
 * Skipped when: the caller asked for quiet/JSON output, stdout is not an
 * interactive terminal (pipes, redirects), CI is set, or the user opted out
 * with AGENTS_NO_NUDGE=1.
 */
export function maybeShowStarNudge(opts = {}) {
    try {
        const show = shouldShowStarNudge({
            quiet: opts.quiet,
            isTTY: Boolean(process.stdout.isTTY),
            ci: Boolean(process.env.CI),
            optedOut: process.env.AGENTS_NO_NUDGE === '1',
            alreadyShown: hasShownStarNudge(),
        });
        if (!show)
            return;
        // Claim the one-time slot with an ATOMIC exclusive create (O_EXCL). The
        // hasShownStarNudge() check above is only a cheap fast-path; the existsSync
        // + write pair is a TOCTOU race across processes, and `agents teams` spawns
        // many at once. The `wx` flag makes the create itself the arbiter: exactly
        // one process succeeds, every other gets EEXIST and stays silent — so the
        // hint prints at most once even under concurrent completions.
        const sentinel = nudgeSentinelPath();
        fs.mkdirSync(path.dirname(sentinel), { recursive: true });
        try {
            fs.writeFileSync(sentinel, new Date().toISOString(), { flag: 'wx' });
        }
        catch (e) {
            if (e.code === 'EEXIST')
                return; // another process won the race
            throw e; // real write failure -> caught by the outer best-effort guard
        }
        console.log('\n' +
            chalk.gray('Enjoying agents-cli? Give it a star to help others find it: ') +
            chalk.cyan(REPO_URL));
    }
    catch {
        /* best-effort: a nudge must never break a successful run */
    }
}
