import { writeSync } from "node:fs";
import { isValidProviderName } from "../config/provider-name";
import { runCatalogAction } from "./catalog-command-result";
import { modelSelectionGuidance, modelSelectionNextSteps } from "./model-selection-guidance";
import { warnIfCodexCatalogRefreshPending } from "./account-catalog-refresh";
import { isCodexResetCreditOperationId } from "../codex/reset-credit-recovery";
import { BROWSER_LAUNCH_FAILED_NOTICE } from "../lib/browser-launch-notice";
import {
  ANTIGRAVITY_RISK_ACK_FIELD,
  ANTIGRAVITY_RISK_CLI_FLAG,
  antigravityRiskDecisionMessage,
  requiresAntigravityRiskAcknowledgement,
  resolveAntigravityRiskAcknowledgement,
} from "../oauth/antigravity-risk";
import { isAgentDriven } from "./agent-driven";
import {
  RuntimeApiError,
  CliUsageError,
  printData,
  readSecretLine,
  rejectArgs,
  runCliAction,
  runtimeRequest,
  runtimeBaseUrl,
  terminalSafeText,
  takeFlag,
  takeOption,
  takeOptionWithSyntax,
  type CliStdin,
  type RuntimeApiDeps,
} from "./runtime-api";

/**
 * Write the whole block to fd 1 synchronously (#1007). `console.log` can
 * buffer behind a pipe, which hid the authorization URL for the entire
 * polling window under non-TTY stdout. A partial write loops; a zero-byte
 * write is a hard failure, never silent progress.
 */
function writeStdoutFully(text: string): void {
  const bytes = Buffer.from(text, "utf8");
  let offset = 0;
  while (offset < bytes.length) {
    const written = writeSync(1, bytes, offset, bytes.length - offset);
    if (written <= 0) throw new CliUsageError("failed to write login instructions to stdout");
    offset += written;
  }
}

const USAGE = `Usage:
  ocx account login <provider> [--id <account-id>] [--reauth] [--device] [--method builder-id|google|github] [--open-browser on|off] [--add-account on|off] [--code -] [--no-wait] [--json] [--accept-antigravity-risk]
  ocx account code <provider> [--flow <flow-id>] [--json]   (reads the code from stdin)
  ocx account cancel <provider> [--flow <flow-id>] [--json] (--flow required for codex)
  ocx account reset-credits <account-id|main> [--consume --yes [--operation-id <uuid>]] [--json]
  ocx account grok-reset-coupons [<account-id>] [--consume --yes [--token-id <token-id>] [--operation-id <uuid>]] [--json]

--device runs the device-code login instead of the browser callback (codex and
xai): use it when the proxy has no browser or nothing can reach its localhost
callback, such as a headless or remote hub. Both print a URL and a short code;
confirm that code in any browser and the login finishes here, so nothing is
pasted back and no local port is used. Each flow polls until its own deadline:
Codex's 15-minute device grant, and xAI's provider-issued expires_in bounded to
30 minutes and to one poll a second.

The redirect URL or authorization code is a short-lived credential. Pipe it in
rather than passing it as an argument, where it lands in shell history and is
visible to anyone who can run ps:
  pbpaste | ocx account code <provider> --flow <flow-id>
  ocx account login <provider> --code -   (same, for the login flow)`;

/**
 * The Codex account pool answers to three spellings, and a user reaches for whichever
 * one they already have a word for. `ocx login codex` routes here as well (dispatch.ts):
 * the pool is deliberately not an `ocx login` provider -- it keeps its own account
 * ledger and runs its browser flow inside the proxy -- but that is an implementation
 * boundary, not something a user should have to know before they can log in.
 */
const CODEX_NAMES = new Set(["openai", "codex", "chatgpt"]);

/** True for every spelling that means "the Codex account pool" rather than an OAuth provider. */
export function isCodexAccountLoginName(name: string): boolean {
  return CODEX_NAMES.has(name.trim().toLowerCase());
}

interface LoginStart {
  url?: string;
  flowId?: string;
  instructions?: string;
  deviceCode?: string;
  /** Whether the host actually opened a browser. Absent from older proxies. */
  browserLaunch?: "started" | "failed" | "skipped";
  /** Set when the proxy's `codexAuthMethod: "device-only"` imposed the device grant (#8082). */
  authMethod?: "device-only";
  method?: "builder-id" | "google" | "github";
  userCode?: string;
  verificationUri?: string;
  verificationUriComplete?: string;
  expiresAt?: number;
  state?: string;
  warning?: string;
}

/** Public handoff only. Never spread runtime bodies into terminal or JSON output. */
function loginObject(value: unknown): Record<string, unknown> {
  if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("Invalid login reply");
  return value as Record<string, unknown>;
}
function publicLoginStart(value: unknown, kiro = false): LoginStart {
  const data = loginObject(value);
  const out: LoginStart = {};
  const strings = kiro
    ? ["flowId", "userCode", "verificationUri", "verificationUriComplete"] as const
    : ["url", "flowId", "instructions", "deviceCode"] as const;
  for (const key of strings) {
    if (data[key] !== undefined && typeof data[key] !== "string") throw new Error("Invalid login handoff field");
    if (typeof data[key] === "string") out[key] = data[key];
  }
  if (typeof data.browserLaunch === "string" && ["started", "failed", "skipped"].includes(data.browserLaunch)) out.browserLaunch = data.browserLaunch as LoginStart["browserLaunch"];
  if (data.authMethod === "device-only") out.authMethod = "device-only";
  if (typeof data.method === "string" && ["builder-id", "google", "github"].includes(data.method)) out.method = data.method as LoginStart["method"];
  if (typeof data.state === "string" && ["pending", "done", "failed", "expired", "cancelled"].includes(data.state)) out.state = data.state as string;
  if (typeof data.warning === "string" && ["duplicate_profile_arn", "manual_review_required"].includes(data.warning)) out.warning = data.warning as string;
  if (typeof data.expiresAt === "number" && Number.isFinite(data.expiresAt)) out.expiresAt = data.expiresAt;
  return out;
}
function publicLoginStatus(value: unknown): Record<string, unknown> {
  const data = loginObject(value);
  const out: Record<string, unknown> = {};
  for (const key of ["accountId", "activeAccountId", "email"]) if (typeof data[key] === "string") out[key] = data[key];
  for (const key of ["loggedIn", "done", "needsReauth", "validationPending", "catalogRefreshPending"]) {
    if (data[key] !== undefined && typeof data[key] !== "boolean") throw new Error("Invalid login completion field");
    if (typeof data[key] === "boolean") out[key] = data[key];
  }
  if (typeof data.status === "string" && ["starting", "pending", "done", "error", "expired", "cancelled", "idle"].includes(data.status)) out.status = data.status;
  if (typeof data.source === "string" && ["oauth", "local-cli", "credential-file", "environment", "manual"].includes(data.source)) out.source = data.source;
  if (data.hint && typeof data.hint === "object" && !Array.isArray(data.hint)) out.hint = publicLoginStart(data.hint);
  if (Array.isArray(data.accounts)) out.accounts = data.accounts.map(value => {
    const row = loginObject(value);
    const account: Record<string, unknown> = {};
    for (const key of ["id", "alias", "email"]) if (typeof row[key] === "string") account[key] = row[key];
    for (const key of ["active", "needsReauth"]) if (typeof row[key] === "boolean") account[key] = row[key];
    if (row.needsReauthReason === "verify_account") account.needsReauthReason = "verify_account";
    if (row.needsReauthReason === "verify_account" && row.needsReauthCause === "tos_violation") account.needsReauthCause = "tos_violation";
    if (typeof row.expiresAt === "number" && Number.isFinite(row.expiresAt)) account.expiresAt = row.expiresAt;
    if (row.plan === null) account.plan = null;
    return account;
  });
  return out;
}
function loginBoolean(args: string[], flag: string): boolean | undefined {
  const option = takeOptionWithSyntax(args, flag);
  if (!option) return undefined;
  if (option.value !== "on" && option.value !== "off") throw new CliUsageError(`${flag} must be on or off`, USAGE);
  return option.value === "on";
}
function writeLoginInstructions(lines: readonly string[]): void {
  const block = lines.filter(line => line !== "").flatMap(line => line.split("\n")).map(terminalSafeText).join("\n");
  if (block) writeStdoutFully(`${block}\n`);
}

/**
 * Said only when the host could not open a browser (#5261).
 *
 * Without it, a failed launch is indistinguishable from a successful one: the URL is printed
 * either way, so the user waits at a terminal that looks like it is working. Names the fixed
 * callback port because that is the part people cannot guess — ChatGPT supplies the redirect
 * URI, so the flow cannot move to a free port, and `--device` is the way around it.
 *
 * Extends the shared notice rather than repeating it: only the second line is specific to this
 * flow, and the first is the sentence every other login prints for the same failure.
 */
export const BROWSER_LAUNCH_FAILED_HINT =
  BROWSER_LAUNCH_FAILED_NOTICE
  + "\n   If nothing on this machine can reach http://localhost:1455, rerun with --device instead.";

/** `-` means "read it from stdin", the documented way to pass a code silently. */
const STDIN_SENTINEL = "-";

/** Providers whose ONLY login is already a device flow; --device is redundant, not wrong. */
const DEVICE_NATIVE_PROVIDERS = new Set(["kimi", "nous", "github-copilot"]);
/** Providers with a device grant besides their browser login; --device selects it. */
const DEVICE_OPT_IN_PROVIDERS = new Set(["xai"]);
/**
 * The native Kiro handoff, rendered for a terminal.
 *
 * Every field here is provider- or proxy-supplied, so they go through the same sanitizer as the
 * OAuth CLI's instructions rather than a local strip regex: a narrower strip both left characters
 * reachable (the Arabic letter mark, the line separators, the deprecated `U+206A`–`U+206F` bidi
 * controls) and deleted ones that must survive (ZWNJ/ZWJ, which shape Persian and emoji). Escaping
 * keeps the value readable in a pasted report and leaves the layout below intact, because a
 * newline inside a field is escaped rather than allowed to add a line.
 */
export function formatKiroDeviceInstructions(start: { verificationUriComplete?: string; verificationUri?: string; userCode?: string; flowId?: string }): string {
  return [
    terminalSafeText(start.verificationUriComplete ?? start.verificationUri ?? ""),
    start.userCode ? `User code: ${terminalSafeText(start.userCode)}` : "",
    start.flowId ? `Flow: ${terminalSafeText(start.flowId)}` : "",
  ].filter(Boolean).join("\n");
}

const ARGV_WARNING =
  "warning: the authorization code was passed as a command-line argument, so it is now in your shell history and was visible in the process list while this ran. Pipe it on stdin instead, or pass `-` to read from stdin.";

/**
 * Resolve the code, preferring stdin.
 *
 * Never logs the value itself — the warning names the exposure, not the
 * credential. What this closes is shell history, `ps`, and this program's own
 * output; a code pasted at an interactive prompt is still visible on the
 * terminal, exactly as it is in the existing interactive login.
 */
async function resolveCode(
  supplied: { value: string; inline: boolean } | undefined,
  deps: RuntimeApiDeps,
  required: boolean,
): Promise<string | undefined> {
  if (supplied && supplied.value !== STDIN_SENTINEL) {
    console.error(ARGV_WARNING);
    return supplied.value;
  }
  if (!supplied && !required) return undefined;
  const input: CliStdin = deps.stdinImpl ?? process.stdin;
  if (input.isTTY) console.error("Paste the redirect URL or authorization code, then press Enter:");
  return await readSecretLine(deps, "authorization code");
}

const CODEX_LOGIN_CANCEL_TIMEOUT_MS = 2_000;

async function cancelStartedCodexLogin(flowId: string | undefined, deps: RuntimeApiDeps): Promise<boolean> {
  if (!flowId) return false;
  try {
    await runtimeRequest("/api/codex-auth/login/cancel", {
      method: "POST", redirect: "error", body: JSON.stringify({ flowId }),
      signal: AbortSignal.timeout(CODEX_LOGIN_CANCEL_TIMEOUT_MS),
    }, deps);
    return true;
  } catch {
    return false;
  }
}

function codexLoginCancelFailure(flowId: string | undefined): string {
  return `Could not cancel the started login flow.${flowId ? ` Flow: ${terminalSafeText(flowId)}` : ""}`;
}

async function login(argv: string[], deps: RuntimeApiDeps): Promise<number | void> {
  const args = [...argv];
  const provider = args.shift()?.trim().toLowerCase();
  const wantsJson = takeFlag(args, "--json");
  const noWait = takeFlag(args, "--no-wait");
  const reauth = takeFlag(args, "--reauth");
  const device = takeFlag(args, "--device");
  const method = takeOption(args, "--method");
  const id = takeOption(args, "--id");
  const suppliedCode = takeOptionWithSyntax(args, "--code");
  const openBrowser = loginBoolean(args, "--open-browser");
  const addAccount = loginBoolean(args, "--add-account");
  const acceptAntigravityRisk = takeFlag(args, ANTIGRAVITY_RISK_CLI_FLAG);
  if (provider && !isValidProviderName(provider)) throw new CliUsageError("invalid provider name", USAGE);
  if (!provider) throw new CliUsageError("provider is required", USAGE);
  const antigravityRisk = requiresAntigravityRiskAcknowledgement(provider);
  if (acceptAntigravityRisk && !antigravityRisk) throw new CliUsageError(`${ANTIGRAVITY_RISK_CLI_FLAG} only applies to google-antigravity`, USAGE);
  if (method !== undefined) {
    if (provider !== "kiro" || !["builder-id", "google", "github"].includes(method)) {
      throw new CliUsageError("--method requires kiro and builder-id, google, or github", USAGE);
    }
    if (reauth || id) throw new CliUsageError("native Kiro device login only adds accounts; remove and re-add to reauthenticate", USAGE);
    if (openBrowser !== undefined || addAccount !== undefined) throw new CliUsageError("native Kiro --method does not accept --open-browser or --add-account", USAGE);
    if (device || suppliedCode) throw new CliUsageError("--method cannot be combined with --device or --code", USAGE);
  }
  // A malformed leftover may contain pasted authorization input, even inside an
  // unknown flag. New login parsing reports only a fixed usage diagnostic.
  if (args.length) throw new CliUsageError("Unexpected arguments or repeated options", USAGE);
  // kimi, nous, and github-copilot are already device flows, so --device is a
  // true statement about them and is accepted as a no-op rather than an error.
  // Anything else has no device grant at all and must fail loudly.
  if (device && !CODEX_NAMES.has(provider) && !DEVICE_NATIVE_PROVIDERS.has(provider) && !DEVICE_OPT_IN_PROVIDERS.has(provider)) {
    throw new CliUsageError("--device is not supported for this provider", USAGE);
  }
  // A device grant never asks for a pasted code: the CLI would have spent the caller's stdin, and
  // the proxy would have accepted the paste into a flow with nothing to do with it (the device flow
  // is not a code flow, so `submitLoginCode` stashes it and the login still waits for approval).
  if (device && suppliedCode) throw new CliUsageError("--code cannot be combined with --device", USAGE);
  if (provider === "xai" && device && wantsJson && !noWait) throw new CliUsageError("xAI device login with --json requires --no-wait; use the returned handoff to complete authorization", USAGE);
  if (addAccount !== undefined && (reauth || CODEX_NAMES.has(provider))) throw new CliUsageError("--add-account is only valid for fresh provider OAuth login", USAGE);
  if (openBrowser !== undefined && (device || DEVICE_NATIVE_PROVIDERS.has(provider))) throw new CliUsageError("--open-browser is not supported for device login", USAGE);
  if (!CODEX_NAMES.has(provider) && id && !reauth) throw new CliUsageError("--id is only valid with --reauth for provider OAuth accounts", USAGE);
  // Antigravity can get the Google account disabled: warn and get an answer before anything
  // else touches stdin or the proxy. A refusal here sends no request at all.
  if (antigravityRisk) {
    const decision = await resolveAntigravityRiskAcknowledgement({
      flag: acceptAntigravityRisk,
      // The question goes to stderr, so a redirected stderr is not a terminal a person can answer.
      interactive: (deps.stdinImpl ?? process.stdin).isTTY === true
        && (deps.antigravityRisk?.promptVisible ?? (() => process.stderr.isTTY === true))(),
      agentDriven: (deps.antigravityRisk?.agentDriven ?? isAgentDriven)(),
      warn: text => console.error(text),
      ask: async question => {
        process.stderr.write(question);
        return await readSecretLine(deps, "confirmation");
      },
    });
    if (decision !== "accepted") throw new CliUsageError(antigravityRiskDecisionMessage(decision));
  }
  // Only resolve when --code was actually given: a plain `ocx account login`
  // opens the browser flow and polls, and must not block on stdin.
  // Codex resolves a stdin code only after the start answer: under `codexAuthMethod: "device-only"`
  // a pasted code is refused, and an open stdin with no data must not hold that refusal back (#8082).
  let code = CODEX_NAMES.has(provider) ? undefined : await resolveCode(suppliedCode, deps, false);
  const pinned = { ...deps, baseUrl: await runtimeBaseUrl(deps) };

  if (method) {
    const start = publicLoginStart(await runtimeRequest<unknown>("/api/oauth/login", {
      method: "POST", redirect: "error", body: JSON.stringify({ provider: "kiro", method }),
    }, pinned), true);
    if (!wantsJson) {
      const block = formatKiroDeviceInstructions(start);
      if (block) writeStdoutFully(`${block}\n`);
    }
    if (noWait) { printData(start, wantsJson, []); return; }
    if (!start.flowId) throw new CliUsageError("Kiro device login did not return a flow id");
    for (let attempt = 0; attempt < 450; attempt++) {
      await Bun.sleep(2_000);
      const state = publicLoginStart(await runtimeRequest<unknown>(`/api/oauth/status?provider=kiro&flowId=${encodeURIComponent(start.flowId)}`, { redirect: "error" }, pinned), true);
      if (state.state === "done") {
        printData(state, wantsJson, ["Logged in to kiro.", ...(state.warning ? [`Warning: ${state.warning}`] : [])]);
        return;
      }
      if (state.state === "failed" || state.state === "expired" || state.state === "cancelled") {
        throw new CliUsageError(`Kiro device login ${state.state}`);
      }
    }
    throw new CliUsageError("Kiro device login timed out");
  }

  if (CODEX_NAMES.has(provider)) {
    const start = publicLoginStart(await runtimeRequest<unknown>("/api/codex-auth/login", {
      method: "POST", redirect: "error",
      body: JSON.stringify({
        ...(id ? { id } : {}),
        ...(reauth ? { reauth: true } : {}),
        ...(device ? { device: true } : {}),
        ...(openBrowser === undefined ? {} : { openBrowser }),
      }),
    }, pinned));
    // `codexAuthMethod: "device-only"` (#8082) imposes the device grant after the request is sent.
    // A JSON wait prints nothing until the end and a pasted code has no flow to complete, so either
    // would leave the person without the URL and code: cancel the flow and say what works instead.
    if (start.authMethod === "device-only" && (suppliedCode !== undefined || (wantsJson && !noWait))) {
      const cancelled = await cancelStartedCodexLogin(start.flowId, pinned);
      const advice = suppliedCode !== undefined
        ? "--code cannot be used; run the login without --code and enter the device code at the returned URL"
        : "with --json, add --no-wait and complete the login with the returned url and deviceCode";
      const cleanup = cancelled ? "" : ` ${codexLoginCancelFailure(start.flowId)}`;
      throw new CliUsageError(`This proxy requires device code login (codexAuthMethod: device-only), so ${advice}.${cleanup}`, USAGE);
    }
    try {
      code = await resolveCode(suppliedCode, deps, false);
    } catch (error) {
      if (!await cancelStartedCodexLogin(start.flowId, pinned)) {
        console.error(codexLoginCancelFailure(start.flowId));
      }
      throw error;
    }
    if (!wantsJson) {
      // One atomic pre-poll block, flushed synchronously so a piped parent
      // reads the URL before the polling window starts (#1007).
      writeLoginInstructions([
        start.url ? `Open this URL to sign in:\n${terminalSafeText(start.url)}` : "",
        start.deviceCode ? `Device code: ${terminalSafeText(start.deviceCode)}` : "",
        start.authMethod === "device-only" ? "Device code login is required by this proxy (codexAuthMethod: device-only)." : "",
        start.instructions ?? "",
        start.browserLaunch === "failed" ? BROWSER_LAUNCH_FAILED_HINT : "",
        start.flowId ? `Flow: ${terminalSafeText(start.flowId)}` : "",
      ]);
    }
    if (code && !start.flowId) throw new Error("Code submission requires a returned flow id");
    if (code && start.flowId) {
      const submitted = await runtimeRequest("/api/codex-auth/login/code", {
        method: "POST", redirect: "error",
        body: JSON.stringify({ flowId: start.flowId, input: code }),
      }, pinned);
      if (loginObject(submitted).ok !== true) throw new Error("Code submission was not accepted");
    }
    if (noWait) {
      printData({ ...start, modelSelection: modelSelectionNextSteps("openai", true) }, wantsJson, modelSelectionGuidance("openai", true));
      return;
    }
    if (!start.flowId) throw new CliUsageError("login did not return a flow id");
    // A device login is deliberately slow: the user leaves this machine to
    // enter the code elsewhere. Match the 15-minute grant instead of giving up
    // at minute five while it is still valid, plus settlement margin for the
    // token exchange and credential write after the final poll.
    // The proxy may impose the device grant on a request that did not ask for it
    // (`codexAuthMethod: "device-only"`, #8082), so the returned code counts too.
    const maxAttempts = device || start.deviceCode ? 480 : 150;
    for (let attempt = 0; attempt < maxAttempts; attempt++) {
      await Bun.sleep(2_000);
      const state = publicLoginStatus(await runtimeRequest<unknown>(
        `/api/codex-auth/login-status?flowId=${encodeURIComponent(start.flowId)}${id ? `&accountId=${encodeURIComponent(id)}` : ""}${reauth ? "&reauth=1" : ""}`,
        { redirect: "error" }, pinned,
      ));
      if (start.flowId) state.flowId = start.flowId;
      if (state.status === "done") {
        if (state.validationPending === true) {
          printData({ ...state, recoveryCommand: "ocx gui", recoveryAction: "After quota recovers, click Refresh quotas in the dashboard Codex account pool." }, wantsJson, [
            "Account registered; validation pending (routing disabled).",
            "After quota recovers, open 'ocx gui' and click Refresh quotas to complete validation.",
          ]);
        } else {
          printData({ ...state, modelSelection: modelSelectionNextSteps("openai") }, wantsJson, [`Logged in${state.email ? ` as ${String(state.email)}` : ""}.`, ...modelSelectionGuidance("openai")]);
        }
        if (!wantsJson) warnIfCodexCatalogRefreshPending(state);
        return state.validationPending === true || state.catalogRefreshPending === true ? 1 : 0;
      }
      if (state.status === "error" || state.status === "expired" || state.status === "cancelled") {
        throw new Error("Login failed or expired");
      }
    }
    throw new CliUsageError("login timed out");
  }

  const start = publicLoginStart(await runtimeRequest<unknown>("/api/oauth/login", {
    method: "POST", redirect: "error",
    body: JSON.stringify({ provider, addAccount: addAccount ?? !reauth, ...(device && DEVICE_OPT_IN_PROVIDERS.has(provider) ? { device: true } : {}), ...(openBrowser === undefined ? {} : { openBrowser }), ...(reauth && id ? { accountId: id, reauth: true } : {}), ...(antigravityRisk ? { [ANTIGRAVITY_RISK_ACK_FIELD]: true } : {}) }),
  }, pinned));
  const xaiDevice = provider === "xai" && device;
  const managedM365 = provider === "m365-copilot";
  if (managedM365 && !start.flowId?.trim()) throw new CliUsageError("Running proxy did not return a Microsoft 365 Copilot login flow ID.");
  const scopedFlow = xaiDevice || managedM365;
  if (xaiDevice && (!start.flowId?.trim() || !start.url?.trim() || !start.deviceCode?.trim())) {
    let cleanupFailed = false;
    try {
      await runtimeRequest("/api/oauth/login/cancel", {
        method: "POST", redirect: "error",
        body: JSON.stringify({ provider, ...(start.flowId?.trim() ? { flowId: start.flowId } : {}) }),
      }, pinned);
    } catch { cleanupFailed = true; }
    throw new CliUsageError(`Running proxy does not support xAI device login; restart or upgrade it.${cleanupFailed ? " Could not cancel the unintended login flow." : ""}`);
  }
  if (!wantsJson) {
    writeLoginInstructions([
      start.url ? `Open this URL to sign in:\n${terminalSafeText(start.url)}` : "",
      provider === "xai" && device ? terminalSafeText(start.instructions ?? "") : start.instructions ?? "",
      start.deviceCode ? `Device code: ${terminalSafeText(start.deviceCode)}` : "",
      managedM365 && start.flowId ? `Flow ID: ${terminalSafeText(start.flowId)}` : "",
    ]);
  }
  if (code) {
    const submitted = await runtimeRequest("/api/oauth/login/code", {
      method: "POST", redirect: "error",
      body: JSON.stringify({ provider, input: code }),
    }, pinned);
    if (loginObject(submitted).ok !== true) throw new Error("Code submission was not accepted");
  }
  if (noWait) {
    printData({ ...start, modelSelection: modelSelectionNextSteps(provider, true) }, wantsJson, modelSelectionGuidance(provider, true));
    return;
  }
  // Outlast the xAI device grant and the proxy-owned M365 browser session.
  for (let attempt = 0; attempt < (device ? 930 : managedM365 ? 375 : 100); attempt++) {
    await Bun.sleep(2_000);
    const raw = await runtimeRequest<Record<string, unknown>>(`/api/oauth/status?provider=${encodeURIComponent(provider)}${scopedFlow ? `&flowId=${encodeURIComponent(start.flowId!)}` : ""}`, { redirect: "error" }, pinned);
    if (loginObject(raw).error) throw new Error("Provider login failed");
    if (scopedFlow && (raw.flowId !== start.flowId || raw.status === "idle")) throw new Error(managedM365 ? "Microsoft 365 Copilot login flow is unavailable" : "Device login flow is unavailable");
    const state = publicLoginStatus(raw);
    if (state.done === true && state.loggedIn !== true && !(scopedFlow && raw.status === "complete")) throw new Error("Provider login ended without authentication");
    // A device login adds an account, so an already signed-in provider is not done until its flow is.
    if (scopedFlow ? raw.status === "complete" : state.loggedIn === true && (!device || state.done === true)) {
      printData({ ...state, modelSelection: modelSelectionNextSteps(provider) }, wantsJson, [`Logged in to ${provider}.`, ...modelSelectionGuidance(provider)]);
      return;
    }
  }
  if (managedM365) await runtimeRequest("/api/oauth/login/cancel", {
    method: "POST", redirect: "error", body: JSON.stringify({ provider, flowId: start.flowId }),
  }, pinned);
  throw new CliUsageError("login timed out");
}

async function code(argv: string[], deps: RuntimeApiDeps): Promise<void> {
  const args = [...argv];
  const provider = args.shift()?.trim().toLowerCase();
  // Flags first. Taking the positional before parsing them made
  // `ocx account code openai --flow f1` read `--flow` as the code and then
  // reject `f1` as an unexpected argument.
  const wantsJson = takeFlag(args, "--json");
  const flowId = takeOption(args, "--flow");
  const suppliedCode = takeOptionWithSyntax(args, "--code");
  // An unknown flag is not a credential. Without this guard `--nope` becomes
  // the positional code and the real complaint ("unexpected argument") is
  // replaced by a confusing one about how the code was passed.
  const positional = args[0]?.startsWith("--") ? undefined : args.shift();
  if (!provider) throw new CliUsageError("provider is required", USAGE);
  // A second positional here is most likely the code, split by an unquoted
  // space or a stray shell expansion. Naming it back would put it on stderr.
  rejectArgs(args, USAGE, { redactValues: true });
  if (suppliedCode && positional !== undefined) {
    throw new CliUsageError("pass the code either positionally or with --code, not both", USAGE);
  }
  const input = await resolveCode(
    suppliedCode ?? (positional === undefined ? undefined : { value: positional, inline: false }),
    deps,
    true,
  );
  if (!input) throw new CliUsageError("provider and redirect/code are required", USAGE);
  const path = CODEX_NAMES.has(provider) ? "/api/codex-auth/login/code" : "/api/oauth/login/code";
  if (CODEX_NAMES.has(provider) && !flowId) throw new CliUsageError("Codex login code requires --flow <flow-id>", USAGE);
  const body = CODEX_NAMES.has(provider) ? { flowId, input } : { provider, input };
  const result = await runtimeRequest(path, { method: "POST", body: JSON.stringify(body) }, deps);
  printData(result, wantsJson, ["Login code submitted."]);
}

async function cancel(argv: string[], deps: RuntimeApiDeps): Promise<void> {
  const args = [...argv];
  const provider = args.shift()?.trim().toLowerCase();
  const wantsJson = takeFlag(args, "--json");
  const flowId = takeOption(args, "--flow")?.trim();
  if (!provider) throw new CliUsageError("provider is required", USAGE);
  rejectArgs(args, USAGE);
  const codex = CODEX_NAMES.has(provider);
  if (!flowId && (codex || provider === "m365-copilot")) {
    throw new CliUsageError(`${codex ? "Codex" : "Microsoft 365 Copilot"} login cancel requires --flow <flow-id> (printed by 'ocx account login').`, USAGE);
  }
  const result = await runtimeRequest(codex ? "/api/codex-auth/login/cancel" : "/api/oauth/login/cancel", {
    method: "POST",
    body: JSON.stringify(codex ? { flowId } : { provider, ...((provider === "kiro" || provider === "xai" || provider === "m365-copilot") && flowId ? { flowId } : {}) }),
  }, deps);
  printData(result, wantsJson, [`Cancelled ${provider} login.`]);
}

async function resetCredits(argv: string[], deps: RuntimeApiDeps): Promise<void> {
  const args = [...argv];
  const rawId = args.shift()?.trim();
  const wantsJson = takeFlag(args, "--json");
  const consume = takeFlag(args, "--consume");
  const yes = takeFlag(args, "--yes");
  // Before rejectArgs: takeOption splices its two tokens out of `args`.
  const operationId = takeOption(args, "--operation-id");
  if (!rawId) throw new CliUsageError("account id is required", USAGE);
  if (consume && !yes) throw new CliUsageError("consuming a reset credit requires --yes", USAGE);
  if (operationId !== undefined && !consume) {
    throw new CliUsageError("--operation-id requires --consume", USAGE);
  }
  if (operationId !== undefined && !isCodexResetCreditOperationId(operationId)) {
    throw new CliUsageError("--operation-id must be a UUIDv4", USAGE);
  }
  rejectArgs(args, USAGE);
  const accountId = rawId === "main" ? "__main__" : rawId;
  const result = consume
    ? await runtimeRequest("/api/codex-auth/reset-credits/consume", {
      method: "POST",
      // Spread, not `operationId: undefined`: the server distinguishes an absent
      // key (legacy random id) from a caller who asked for a stable identity.
      body: JSON.stringify({ accountId, ...(operationId === undefined ? {} : { operationId }) }),
    }, deps)
    : await runtimeRequest(`/api/codex-auth/reset-credits?accountId=${encodeURIComponent(accountId)}`, {}, deps);
  printData(result, wantsJson);
}

/** Codes that leave a coupon operation unconfirmed: the caller must keep its operationId. */
const UNCONFIRMED_COUPON_CODES: ReadonlySet<string> = new Set([
  "attempt_unresolved", "attempt_in_progress", "attempt_reconcile_failed", "operation_state_changed",
  "redeem_failed", "attempt_mark_failed", "operation_token_mismatch",
]);
/** Pre-dispatch ledger refusals: unconfirmed when anchored to an operation, never a settled code. */
const LEDGER_REFUSAL_CODES: ReadonlySet<string> = new Set(["ledger_unavailable", "capacity"]);
/** The only codes the ledger records as a definitive outcome; any other settled code is unconfirmed. */
const DEFINITIVE_SETTLED_CODES: ReadonlySet<string> = new Set([
  "redeemed", "auth_failed", "fetch_resets_failed", "no_coupons_available", "coupon_unavailable", "token_unresolved",
]);

async function grokResetCoupons(argv: string[], deps: RuntimeApiDeps): Promise<void> {
  const args = [...argv];
  // The account id is optional here (the server falls back to the selected xAI
  // account), so a flag-shaped first token must not be swallowed as the id:
  // `grok-reset-coupons --consume` has to reach the --yes gate, not become a
  // read of account "--consume".
  const rawId = args[0]?.startsWith("--") ? undefined : args.shift()?.trim();
  const wantsJson = takeFlag(args, "--json");
  const consume = takeFlag(args, "--consume");
  const yes = takeFlag(args, "--yes");
  // Before rejectArgs: takeOption splices its two tokens out of `args`.
  const tokenId = takeOption(args, "--token-id");
  const operationId = takeOption(args, "--operation-id");
  if (consume && !yes) throw new CliUsageError("consuming a Grok reset coupon requires --yes", USAGE);
  if (operationId !== undefined && !consume) {
    throw new CliUsageError("--operation-id requires --consume", USAGE);
  }
  if (tokenId !== undefined && !consume) {
    throw new CliUsageError("--token-id requires --consume", USAGE);
  }
  if (operationId !== undefined && !isCodexResetCreditOperationId(operationId)) {
    throw new CliUsageError("--operation-id must be a UUIDv4", USAGE);
  }
  rejectArgs(args, USAGE);
  const accountId = rawId ? (rawId === "main" ? "__main__" : rawId) : undefined;
  const effectiveOperationId = consume ? operationId ?? crypto.randomUUID() : undefined;
  const unconfirmed = () => new RuntimeApiError(`Coupon redemption outcome is unconfirmed. Preserve --operation-id ${effectiveOperationId}; reuse it to inspect this attempt and do not create a replacement operation.`, 503, null);
  let result: unknown;
  try {
    result = consume
      ? await runtimeRequest("/api/grok/reset-coupons/consume", {
        method: "POST", body: JSON.stringify({ accountId, tokenId, operationId: effectiveOperationId }),
      }, deps)
      : await runtimeRequest(`/api/grok/reset-coupons${accountId ? `?accountId=${encodeURIComponent(accountId)}` : ""}`, {}, deps);
  } catch (error) {
    const apiError = error instanceof RuntimeApiError ? error : undefined;
    const body = apiError?.body as { operationId?: unknown; error?: { code?: string } } | null | undefined;
    const code = body && typeof body === "object" ? body.error?.code : undefined;
    const carriesOperation = !!body && typeof body === "object" && typeof body.operationId === "string";
    if (consume && (!apiError || (!apiError.code && (
      (!code && apiError.status >= 500) || UNCONFIRMED_COUPON_CODES.has(code ?? "")
      || (LEDGER_REFUSAL_CODES.has(code ?? "") && carriesOperation)
    )))) {
      throw new RuntimeApiError(`Coupon redemption outcome is unconfirmed. Preserve --operation-id ${effectiveOperationId}; reuse it to inspect this attempt and do not create a replacement operation.`, apiError?.status ?? 503, apiError?.body ?? null);
    }
    throw error;
  }
  if (consume) {
    // A 200 can replay a recorded refusal; only "redeemed" is a successful spend.
    const settled = result && typeof result === "object" ? (result as { code?: unknown }).code : undefined;
    if (typeof settled !== "string" || !DEFINITIVE_SETTLED_CODES.has(settled)) throw unconfirmed();
    if (settled !== "redeemed") {
      printData(result, wantsJson);
      throw new RuntimeApiError(`Coupon was not redeemed (${settled}).`, 409, result);
    }
  }
  printData(result, wantsJson);
}

export async function handleAccountAuthCommand(sub: string, argv: string[], deps: RuntimeApiDeps = {}): Promise<number | null> {
  let action: (() => Promise<void>) | undefined;
  if (sub === "login" || sub === "reauth") return runCatalogAction(() => login(sub === "reauth" ? [...argv, "--reauth"] : argv, deps));
  else if (sub === "code") action = () => code(argv, deps);
  else if (sub === "cancel") action = () => cancel(argv, deps);
  else if (sub === "reset-credits") action = () => resetCredits(argv, deps);
  else if (sub === "grok-reset-coupons") action = () => grokResetCoupons(argv, deps);
  if (!action) return null;
  return runCliAction(action);
}

export const ACCOUNT_AUTH_USAGE = USAGE;
