import { isValidModelDiscoveryModelId, MODEL_DISCOVERY_MAX_MODELS } from "./model-discovery-limits";
import { captureModelCacheGeneration, isModelCacheGenerationCurrent } from "../codex/model-cache";
import { getConfigDir } from "../config/paths";
import { readAntigravityWireSnapshot, writeAntigravityWireSnapshot, type AntigravitySuffixMap } from "./antigravity-wire-snapshot";

// Google Antigravity (Cloud Code Assist) bundled model list.
//
// Single source of truth: the Antigravity `:fetchAvailableModels` backend, the same one the `agy`
// CLI resolves labels against. The ids below separate CCA wire ids, collapsed picker entries,
// and hidden compatibility aliases for saved selections. The CCA envelope's `model` field must
// receive the wire id (for example "Gemini 3.1 Pro (High)" => gemini-pro-agent), while the
// picker collapses complete low/medium/high families discovered by CCA, including new versions.
// Partial families remain wire-addressable rather than advertising an invented effort ladder.

// ── Wire IDs (what CCA :fetchAvailableModels returns) ──

/** Current Antigravity Flash generation. */
const GEMINI_FLASH_CURRENT = "gemini-3.8-flash";

/**
 * Gemini 4 Argon. Like 3.8 it publishes one wire id per tier (`-low/-medium/-high`) and no
 * `-tiered` row, verified 2026-10-14 against live CCA. CCA serves these ids only to the CLI
 * User-Agent (see antigravityUserAgentForWireModel), and only to AI Ultra / Enterprise accounts.
 */
const GEMINI_ARGON = "gemini-4-argon";

/**
 * Claude 5.5 on Antigravity: one wire id per tier (`-low/-medium/-high`), each 1,000,000 tokens
 * with image input, observed in live CCA discovery on 2026-10-03
 * (devlog/_fin/261003_release_lane_c). They ride the IDE User-Agent like every non-Argon model.
 */
const CLAUDE_OPUS_55 = "claude-opus-5-5";
const CLAUDE_SONNET_55 = "claude-sonnet-5-5";

/**
 * Previous Flash generation — still served, still picker-visible.
 *
 * 3.6 vanished from CCA the moment 3.7 shipped, which is why RETIRED_FLASH_TIERS exists. 3.8
 * did not do that: Google documents 3.7 Flash as "remains fully supported", and a 2026-09-03
 * :fetchAvailableModels call returns 3.8, 3.7 AND 3.6 wire ids together. Retiring 3.7 here
 * would strand a model the backend is actively serving.
 */
const GEMINI_FLASH_PREVIOUS = "gemini-3.7-flash";

/**
 * Wire ID that CCA accepts for the RETIRED-tier redirect target (currently 3.7).
 *
 * Google renamed 3.7 to carry a `-tiered` suffix; the picker-visible ID stays
 * `gemini-3.7-flash` (stripped by `pickerModelIdForDiscoveredWireId`). This constant is named
 * for its ROLE, not for the current generation: 3.8 is current and has no `-tiered` id, so a
 * name like GEMINI_FLASH_WIRE_ID would now point readers at the wrong model.
 */
const GEMINI_RETIRED_FLASH_TARGET_WIRE_ID = "gemini-3.7-flash-tiered";

/**
 * Retired Flash ids → the reasoning tier they used to encode.
 *
 * Google pulls the previous Flash model from CCA almost immediately when the next
 * one ships, so a saved selection cannot keep pointing at it. A flat alias would
 * strand the user's tier: `resolveAntigravityEffortWireModel` rule 1 treats any
 * alias as "the suffix IS the effort" and deliberately sends no thinkingConfig, so
 * `gemini-3.6-flash-high` would silently become an untiered 3.7 call. Carrying the
 * level here preserves what the user actually chose.
 *
 * 3.7 exposes tiers through `thinkingLevel` on ONE wire id rather than through
 * suffixed wire ids, which is why the mapping is id → level and not id → id.
 */
const RETIRED_FLASH_TIERS: Record<string, string> = {
  // 3.6 generation.
  "gemini-3.6-flash": "medium", // bare base carried a medium default
  "gemini-3.6-flash-low": "low",
  "gemini-3.6-flash-medium": "medium",
  "gemini-3.6-flash-high": "high",
  // 3.5 generation — these already pointed at 3.6 wire ids, which are now dead too.
  "gemini-3.5-flash-extra-low": "low",
  "gemini-3.5-flash-low": "medium",
  "gemini-3.5-flash-mid": "medium",
  "gemini-3.5-flash-high": "high",
  "gemini-3-flash-agent": "high",
};

const ANTIGRAVITY_WIRE_MODELS = [
  "gemini-3.7-flash-tiered",
  "gemini-3.1-pro-low",
  "gemini-pro-agent",
  "gemini-3.1-flash-image",
  "claude-sonnet-4-6",
  "claude-opus-4-6-thinking",
  "gpt-oss-120b-medium",
];

const ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID: Record<string, string> = {
  "claude-opus-5-5-low": CLAUDE_OPUS_55,
  "claude-opus-5-5-medium": CLAUDE_OPUS_55,
  "claude-opus-5-5-high": CLAUDE_OPUS_55,
  "claude-sonnet-5-5-low": CLAUDE_SONNET_55,
  "claude-sonnet-5-5-medium": CLAUDE_SONNET_55,
  "claude-sonnet-5-5-high": CLAUDE_SONNET_55,
  "gemini-4-argon-low": GEMINI_ARGON,
  "gemini-4-argon-medium": GEMINI_ARGON,
  "gemini-4-argon-high": GEMINI_ARGON,
  "gemini-3.8-flash-low": "gemini-3.8-flash",
  "gemini-3.8-flash-medium": "gemini-3.8-flash",
  "gemini-3.8-flash-high": "gemini-3.8-flash",
  "gemini-3.1-pro-low": "gemini-3.1-pro",
  "gemini-pro-agent": "gemini-3.1-pro",
};

const ANTIGRAVITY_WIRE_IDS_BY_PICKER_MODEL: Record<string, string[]> = Object.entries(
  ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID,
).reduce<Record<string, string[]>>((out, [wireId, pickerId]) => {
  (out[pickerId] ??= []).push(wireId);
  return out;
}, {});

const ANTIGRAVITY_DISCOVERY_EFFORTS = ["low", "medium", "high"] as const;
type AntigravityDiscoveryEffort = typeof ANTIGRAVITY_DISCOVERY_EFFORTS[number] | "xhigh";
type AntigravityEffortWireModelIds = Partial<Record<AntigravityDiscoveryEffort, string>>;

function isAntigravityDiscoveryEffort(value: string): value is AntigravityDiscoveryEffort {
  return [...ANTIGRAVITY_DISCOVERY_EFFORTS, "xhigh"].includes(value as AntigravityDiscoveryEffort);
}

function pickerModelIdForDiscoveredWireId(
  wireId: string,
  info: Record<string, unknown>,
  available: ReadonlyMap<string, Record<string, unknown>>,
): string {
  const explicitPickerId = Object.hasOwn(ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID, wireId)
    ? ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID[wireId]
    : undefined;
  if (explicitPickerId) {
    const requiredWireIds = Object.hasOwn(ANTIGRAVITY_WIRE_IDS_BY_PICKER_MODEL, explicitPickerId)
      ? ANTIGRAVITY_WIRE_IDS_BY_PICKER_MODEL[explicitPickerId] ?? []
      : [];
    if (requiredWireIds.every(id => available.has(id))) return explicitPickerId;
  }

  // CCA uses a single `-tiered` row for models whose effort levels ride on the
  // request's thinkingLevel field. Keep this generic so new tiered models do not
  // require another provider-specific ID mapping.
  if (wireId.endsWith("-tiered")) {
    const baseId = wireId.slice(0, -"-tiered".length);
    if (isKnownAntigravityPickerModelId(baseId)) return baseId;
  }

  const effortMatch = /^(.*)-(low|medium|high|xhigh)$/.exec(wireId);
  if (effortMatch) {
    const baseId = effortMatch[1]!;
    if (isValidModelDiscoveryModelId(baseId)
      && ANTIGRAVITY_DISCOVERY_EFFORTS.every(effort => available.has(`${baseId}-${effort}`))) {
      return baseId;
    }
    // A shared display label must not collapse an incomplete family or rename its wire tier.
    return wireId;
  }

  // Display labels are a LAST resort, never a first one. CCA labels a tier row
  // "Gemini 3.7 Flash (High)", which slugs to `gemini-3.7-flash-high` — a per-tier
  // picker row that re-splits exactly the ladder the collapse rules above just
  // joined, and that carries no effort ladder of its own. Consulting the label
  // first (the #1897 regression) turned every collapsed base model back into three
  // suffix rows and stripped reasoning-effort control from the picker.
  //
  // The label still earns its keep where nothing else can speak: an id Google has
  // renamed on the wire while keeping a stable public name.
  const displayModelId = antigravityDisplayModelId(info.displayName, wireId);
  if (displayModelId && !collapsesIntoKnownPickerModel(displayModelId)) return displayModelId;

  return wireId;
}

/**
 * Whether a display-derived id is really a tier of a picker-visible base model.
 *
 * `gemini-3.7-flash-high` looks like a model id and is not one: it is the "high"
 * rung of `gemini-3.7-flash`, whose ladder lives in ANTIGRAVITY_MODEL_EFFORTS.
 * Publishing it as its own row is what breaks effort selection.
 */
function collapsesIntoKnownPickerModel(candidateId: string): boolean {
  const effortMatch = /^(.*)-(low|medium|high)$/.exec(candidateId);
  const baseId = effortMatch?.[1];
  return baseId !== undefined && isKnownAntigravityPickerModelId(baseId);
}

// ── Effort ladders per collapsed base model ──
// Gemini models: effort → wire model suffix (official agy UI pattern).
// Claude Opus: effort → thinkingConfig.thinkingLevel (CLIProxyAPI proven pattern).
export const ANTIGRAVITY_MODEL_EFFORTS: Record<string, string[]> = {
  // No `minimal`: Google documents it as an error for this generation, and CCA exposes only
  // the three tiers. Argon documents the same ladder (`minimal` is an HTTP 400).
  [GEMINI_ARGON]: ["low", "medium", "high"],
  "gemini-3.8-flash": ["low", "medium", "high"],
  [CLAUDE_OPUS_55]: ["low", "medium", "high"],
  [CLAUDE_SONNET_55]: ["low", "medium", "high"],
  "gemini-3.7-flash": ["low", "medium", "high"],
  "gemini-3.1-pro": ["low", "high"],
  "claude-sonnet-4-6": ["low", "medium", "high", "max"],
  "claude-opus-4-6-thinking": ["low", "medium", "high", "max"],
};

// ── Effort → wire model map for Gemini base models ──
const ANTIGRAVITY_EFFORT_WIRE_MAP: Record<string, Record<string, string>> = {
  // 3.8 publishes one wire id per tier and no `-tiered` row, so its efforts ride the suffix.
  // This is the 3.6 shape, not the 3.7 one. Argon has the same shape.
  [GEMINI_ARGON]: {
    low: "gemini-4-argon-low",
    medium: "gemini-4-argon-medium",
    high: "gemini-4-argon-high",
  },
  // Claude 5.5 publishes the same per-tier suffix family.
  [CLAUDE_OPUS_55]: {
    low: "claude-opus-5-5-low",
    medium: "claude-opus-5-5-medium",
    high: "claude-opus-5-5-high",
  },
  [CLAUDE_SONNET_55]: {
    low: "claude-sonnet-5-5-low",
    medium: "claude-sonnet-5-5-medium",
    high: "claude-sonnet-5-5-high",
  },
  "gemini-3.8-flash": {
    low: "gemini-3.8-flash-low",
    medium: "gemini-3.8-flash-medium",
    high: "gemini-3.8-flash-high",
  },
  "gemini-3.1-pro": {
    low: "gemini-3.1-pro-low",
    high: "gemini-pro-agent",
  },
};

/**
 * Base models whose every effort maps to a wire id that ALREADY encodes the tier.
 *
 * Sending `thinkingLevel` beside such a suffix states the effort twice, and CCA does not reject
 * the contradiction — a `-low` wire id paired with `HIGH` returns 200, so the tier that actually
 * ran becomes unknowable from the response. Membership also makes static resolution
 * byte-identical to discovery, except when an opted-in Argon summary needs its matching level.
 *
 * `gemini-3.1-pro` is deliberately absent: its `high` rung is `gemini-pro-agent`, which carries
 * no tier suffix, so there the level is the only thing naming the effort.
 */
const ANTIGRAVITY_SUFFIX_TIER_MODELS = new Set([GEMINI_ARGON, "gemini-3.8-flash", CLAUDE_OPUS_55, CLAUDE_SONNET_55]);

/**
 * Suffix-tier models that withhold `thought: true` text unless the request ALSO names its tier
 * in `thinkingConfig.thinkingLevel`.
 *
 * Measured against live CCA (2026-10-27): `gemini-4-argon-high` with
 * `{includeThoughts: true}` alone answers 200 with `thoughtsTokenCount` in the thousands and
 * zero thought parts — the whole plan arrives as ordinary visible text. Sending
 * `{thinkingLevel: "high", includeThoughts: true}` on the same wire id returns the thought
 * parts. `gemini-3.8-flash-high` does not behave this way: its include-only request still
 * returns thoughts, so it stays out of this set and keeps the one-field shape.
 */
const ANTIGRAVITY_THOUGHT_LEVEL_REQUIRED_SUFFIX_MODELS = new Set([GEMINI_ARGON]);

function suffixThoughtLevel(wireModelId: string, includeThoughts: boolean | undefined): string | undefined {
  if (!includeThoughts) return undefined;
  for (const base of ANTIGRAVITY_THOUGHT_LEVEL_REQUIRED_SUFFIX_MODELS) {
    for (const tier of ANTIGRAVITY_DISCOVERY_EFFORTS) {
      if (wireModelId === `${base}-${tier}`) return tier;
    }
  }
  return undefined;
}

function suffixThoughtWire(wireModelId: string, includeThoughts: boolean | undefined): { wireModelId: string; thinkingLevel?: string } {
  const thinkingLevel = suffixThoughtLevel(wireModelId, includeThoughts);
  return { wireModelId, ...(thinkingLevel ? { thinkingLevel } : {}) };
}

function completeDiscoveredEffortWireModelIds(
  pickerId: string,
  available: ReadonlyMap<string, Record<string, unknown>>,
): AntigravityEffortWireModelIds | undefined {
  const explicitEffortMap = Object.hasOwn(ANTIGRAVITY_EFFORT_WIRE_MAP, pickerId)
    ? ANTIGRAVITY_EFFORT_WIRE_MAP[pickerId] : undefined;
  if (explicitEffortMap && Object.values(explicitEffortMap).every(wireId => available.has(wireId))) {
    return { ...explicitEffortMap, ...(available.has(`${pickerId}-xhigh`) ? { xhigh: `${pickerId}-xhigh` } : {}) };
  }

  if (!isValidModelDiscoveryModelId(pickerId)) return undefined;
  const suffixEffortMap: AntigravityEffortWireModelIds = {};
  for (const effort of ANTIGRAVITY_DISCOVERY_EFFORTS) {
    const wireId = `${pickerId}-${effort}`;
    if (!available.has(wireId)) return undefined;
    suffixEffortMap[effort] = wireId;
  }
  if (available.has(`${pickerId}-xhigh`)) suffixEffortMap.xhigh = `${pickerId}-xhigh`;
  return suffixEffortMap;
}

/**
 * Default rung for a model known only from live discovery. Gemini 4 Hydrogen defaults to high:
 * its low/medium/high/xhigh ladder and high default were verified on Vertex AI by #8583's author
 * (2026-11-13). On CCA it is entitlement-gated: an account whose discovery listed the four
 * `gemini-4-hydrogen-*` tiers answered an xhigh request with 200 (2026-11-12), so this applies only
 * once that account's CCA discovery lists the family. Other discovered families keep medium when they have it.
 */
const ANTIGRAVITY_DISCOVERED_DEFAULT_EFFORT: Record<string, string> = {
  "gemini-4-hydrogen": "high",
};

/** Default effort the catalog publishes, and routing applies, for a discovered effort family. */
export function discoveredAntigravityDefaultEffort(modelId: string, hasMedium: boolean): string {
  return Object.hasOwn(ANTIGRAVITY_DISCOVERED_DEFAULT_EFFORT, modelId)
    ? ANTIGRAVITY_DISCOVERED_DEFAULT_EFFORT[modelId]!
    : hasMedium ? "medium" : "high";
}

// ── Default effort per Gemini base model ──
const ANTIGRAVITY_DEFAULT_EFFORT: Record<string, string> = {
  // Google's documented thinking_level default, and the tier CCA marks `recommended`.
  [GEMINI_ARGON]: "medium",
  "gemini-3.8-flash": "medium",
  // Matches the discovered-family default (medium), so static and live resolution agree.
  [CLAUDE_OPUS_55]: "medium",
  [CLAUDE_SONNET_55]: "medium",
  "gemini-3.1-pro": "high",
};

/**
 * Gemini base models whose efforts ride on `thinkingLevel` against a single wire id
 * instead of suffixed wire ids, with the level applied when the caller names none.
 */
const ANTIGRAVITY_THINKING_LEVEL_MODELS: Record<string, string> = {
  "gemini-3.7-flash": "medium",
};

// `minimal` is deliberately absent: Google documents it as unsupported for the current
// Flash generation, where it is an error rather than a quieter tier.
const ANTIGRAVITY_THINKING_LEVELS = new Set(["low", "medium", "high"]);

/**
 * Picker-visible model IDs whose CCA wire ID differs (the `-tiered` rename).
 * Models not listed here use themselves as the wire ID.
 */
const ANTIGRAVITY_PICKER_TO_WIRE: Record<string, string> = {
  "gemini-3.7-flash": GEMINI_RETIRED_FLASH_TARGET_WIRE_ID,
};

/** Map a picker-visible base model to its CCA wire ID. Identity when no mapping exists. */
function pickerToWireId(pickerId: string): string {
  return ANTIGRAVITY_PICKER_TO_WIRE[pickerId] ?? pickerId;
}

/** Requested efforts above `high`: they select a discovered xhigh tier when one exists, else `high`. */
const ANTIGRAVITY_ABOVE_HIGH_EFFORTS = new Set(["xhigh", "max", "ultra"]);

function resolveAntigravityThinkingLevel(effort: string): string | undefined {
  if (ANTIGRAVITY_ABOVE_HIGH_EFFORTS.has(effort)) return "high";
  return ANTIGRAVITY_THINKING_LEVELS.has(effort) ? effort : undefined;
}

// ── Visible client aliases (kept for saved-config compat, not picker-visible) ──
const ANTIGRAVITY_VISIBLE_MODEL_ALIASES: Record<string, string> = {
  "gemini-3.1-pro-high": "gemini-pro-agent",
  "gemini-3.1-pro-preview": "gemini-pro-agent",
};

// ── Hidden compatibility aliases for saved selections ──
// Wire suffix IDs are identity aliases — they resolve to themselves so saved configs
// with explicit suffixes (e.g. gemini-3.6-flash-low) continue to work.
const ANTIGRAVITY_COMPATIBILITY_MODEL_ALIASES: Record<string, string> = {
  "gemini-3.1-pro-low": "gemini-3.1-pro-low",
  "gemini-pro-agent": "gemini-pro-agent",
  // ── Retired Flash generations ──
  // Google takes the previous Antigravity Flash model offline almost immediately
  // once its successor ships, so 3.6 (and the 3.5 ids that used to land on it)
  // route to 3.7. These stay in the alias map — not only in the tier map below —
  // because `parseAntigravityAvailableModels` uses THIS map to keep a stale CCA
  // payload from republishing a dead wire id as a picker row.
  ...Object.fromEntries(
    Object.keys(RETIRED_FLASH_TIERS).map(retired => [retired, GEMINI_RETIRED_FLASH_TARGET_WIRE_ID]),
  ),
};

export const ANTIGRAVITY_MODEL_ALIASES: Record<string, string> = {
  ...ANTIGRAVITY_VISIBLE_MODEL_ALIASES,
  ...ANTIGRAVITY_COMPATIBILITY_MODEL_ALIASES,
};

// Static picker lineup: collapsed base models only, exactly "3.8 / 4 / 5.5 / 5.5" (2026-10-14).
// Anything else reaches the picker only when live discovery returns it.
export const ANTIGRAVITY_MODELS = [
  GEMINI_FLASH_CURRENT,
  GEMINI_ARGON,
  CLAUDE_OPUS_55,
  CLAUDE_SONNET_55,
];

/**
 * Every base id the routing and discovery rules recognise: the static lineup plus models that
 * left it. Removing a model from the picker must not change how a saved selection routes or how
 * a discovered row collapses, so these legacy bases stay known here. Live discovery on
 * 2026-10-14 no longer returns gemini-3.7-flash(-tiered) or the gemini-3.1-pro wire ids (both
 * 404), but still returns the Claude 4.6 rows and gpt-oss-120b-medium, which therefore keep
 * appearing through discovery.
 */
export const ANTIGRAVITY_KNOWN_PICKER_MODELS: readonly string[] = [
  ...ANTIGRAVITY_MODELS,
  GEMINI_FLASH_PREVIOUS,
  "gemini-3.1-pro",
  "gemini-3.1-flash-image",
  "claude-sonnet-4-6",
  "claude-opus-4-6-thinking",
  "gpt-oss-120b-medium",
];

function isKnownAntigravityPickerModelId(value: string): boolean {
  return isValidModelDiscoveryModelId(value) && ANTIGRAVITY_KNOWN_PICKER_MODELS.includes(value);
}

// Context windows from the upstream `:fetchAvailableModels` maxTokens per model.
const ANTIGRAVITY_WIRE_MODEL_CONTEXT_WINDOWS: Record<string, number> = {
  "claude-opus-5-5-low": 1_000_000,
  "claude-opus-5-5-medium": 1_000_000,
  "claude-opus-5-5-high": 1_000_000,
  "claude-sonnet-5-5-low": 1_000_000,
  "claude-sonnet-5-5-medium": 1_000_000,
  "claude-sonnet-5-5-high": 1_000_000,
  "gemini-4-argon-low": 1_048_576,
  "gemini-4-argon-medium": 1_048_576,
  "gemini-4-argon-high": 1_048_576,
  "gemini-3.8-flash-low": 1_048_576,
  "gemini-3.8-flash-medium": 1_048_576,
  "gemini-3.8-flash-high": 1_048_576,
  "gemini-3.7-flash-tiered": 1_048_576,
  "gemini-3.1-pro-low": 1_048_576,
  "gemini-pro-agent": 1_048_576,
  "gemini-3.1-flash-image": 1_048_576,
  "claude-sonnet-4-6": 250_000,
  "claude-opus-4-6-thinking": 250_000,
  "gpt-oss-120b-medium": 131_072,
};

export const ANTIGRAVITY_MODEL_CONTEXT_WINDOWS: Record<string, number> = {
  // Collapsed base IDs — explicit entries for the picker.
  [GEMINI_ARGON]: 1_048_576,
  "gemini-3.8-flash": 1_048_576,
  [CLAUDE_OPUS_55]: 1_000_000,
  [CLAUDE_SONNET_55]: 1_000_000,
  "gemini-3.7-flash": 1_048_576,
  "gemini-3.1-pro": 1_048_576,
  // Wire IDs and aliases via derivation.
  ...ANTIGRAVITY_WIRE_MODEL_CONTEXT_WINDOWS,
  ...Object.fromEntries(
    Object.entries(ANTIGRAVITY_MODEL_ALIASES).map(([alias, wire]) => [
      alias,
      ANTIGRAVITY_WIRE_MODEL_CONTEXT_WINDOWS[wire],
    ]),
  ),
};

export const ANTIGRAVITY_MODEL_INPUT_MODALITIES: Record<string, string[]> = {
  [GEMINI_ARGON]: ["text", "image"],
  [CLAUDE_OPUS_55]: ["text", "image"],
  [CLAUDE_SONNET_55]: ["text", "image"],
  // Google documents 3.7 Flash as also accepting video, audio and PDF, but this proxy
  // carries only text and image parts (`OcxImageContent`, src/types.ts) and the Codex
  // catalog normalizes `input_modalities` against a closed enum. Advertising a modality
  // the wire cannot carry would be a promise we break at request time.
  "gemini-3.8-flash": ["text", "image"],
  "gemini-3.7-flash": ["text", "image"],
  "gemini-3.1-pro": ["text", "image"],
  "gemini-3.1-flash-image": ["text", "image"],
  "claude-sonnet-4-6": ["text", "image"],
  "claude-opus-4-6-thinking": ["text", "image"],
  "gpt-oss-120b-medium": ["text"],
};

export interface AntigravityAvailableModel {
  id: string;
  /** CCA model id used by the agent envelope when `id` comes from display metadata. */
  wireModelId: string;
  /** Complete effort-to-wire mapping retained for collapsed discovered tier sets. */
  effortWireModelIds?: AntigravityEffortWireModelIds;
  contextWindow?: number;
  inputModalities?: string[];
}

function antigravityRecord(value: unknown): Record<string, unknown> | undefined {
  return value !== null && typeof value === "object" && !Array.isArray(value)
    ? value as Record<string, unknown>
    : undefined;
}

function antigravityPositiveInteger(value: unknown): number | undefined {
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
}

interface DiscoveredWireModelMapping {
  readonly models: ReadonlyMap<string, string>;
  readonly effortModels: ReadonlyMap<string, AntigravityEffortWireModelIds>;
  readonly generation?: { provider: string; cacheGeneration: string };
}

const discoveredWireModelsByBaseUrl = new Map<string, DiscoveredWireModelMapping | null>();

/**
 * Strip trailing slashes without a backtracking regex.
 *
 * `/\/+$/` is polynomial-ReDoS on attacker-influenceable input (CodeQL js/polynomial-redos):
 * a long run of slashes makes the engine retry every suffix. The base URL comes from provider
 * config, which is not hostile in the ordinary case — but "not hostile today" is a property of
 * the caller, not of this function, and a linear scan costs nothing.
 */
function stripTrailingSlashes(value: string): string {
  let end = value.length;
  while (end > 0 && value.charCodeAt(end - 1) === 47) end -= 1;
  return end === value.length ? value : value.slice(0, end);
}

function antigravityBaseUrlKey(baseUrl: string | undefined): string | undefined {
  if (typeof baseUrl !== "string" || !baseUrl.trim()) return undefined;
  const trimmed = stripTrailingSlashes(baseUrl.trim());
  try {
    const url = new URL(trimmed);
    url.hash = "";
    url.search = "";
    return stripTrailingSlashes(url.toString());
  } catch {
    return trimmed;
  }
}

/** Remember the wire ids returned by one live CCA discovery for request routing. */
export function registerAntigravityDiscoveredWireModels(
  baseUrl: string | undefined,
  models: readonly AntigravityAvailableModel[],
  generation?: { provider: string; cacheGeneration: string },
): void {
  const key = antigravityBaseUrlKey(baseUrl);
  if (!key) return;
  if (generation) {
    if (!isModelCacheGenerationCurrent(generation.provider, generation.cacheGeneration)) return;
    const families: Record<string, AntigravitySuffixMap> = Object.create(null);
    for (const model of models) {
      if (ANTIGRAVITY_DISCOVERY_EFFORTS.every(effort => model.effortWireModelIds?.[effort] === `${model.id}-${effort}`)) {
        families[model.id] = { low: `${model.id}-low`, medium: `${model.id}-medium`, high: `${model.id}-high`, ...(model.effortWireModelIds?.xhigh === `${model.id}-xhigh` ? { xhigh: `${model.id}-xhigh` } : {}) };
      }
    }
    writeAntigravityWireSnapshot(key, { version: 1, provider: generation.provider, families });
  }
  const wireModels = new Map<string, string>();
  const effortModels = new Map<string, AntigravityEffortWireModelIds>();
  for (const model of models) {
    wireModels.set(model.id, model.wireModelId);
    if (model.effortWireModelIds) effortModels.set(model.id, { ...model.effortWireModelIds });
  }
  discoveredWireModelsByBaseUrl.set(`${getConfigDir()}\0${key}`, {
    models: wireModels,
    effortModels,
    ...(generation ? { generation } : {}),
  });
}

function discoveredAntigravityMapping(
  baseUrl: string | undefined,
): DiscoveredWireModelMapping | undefined {
  const key = antigravityBaseUrlKey(baseUrl);
  if (!key) return undefined;
  const scopedKey = `${getConfigDir()}\0${key}`;
  if (!discoveredWireModelsByBaseUrl.has(scopedKey)) {
    const saved = readAntigravityWireSnapshot(key);
    discoveredWireModelsByBaseUrl.set(scopedKey, saved ? {
      models: new Map(Object.entries(saved.families).map(([id, map]) => [id, map.medium])),
      effortModels: new Map(Object.entries(saved.families)),
      generation: { provider: saved.provider, cacheGeneration: captureModelCacheGeneration(saved.provider) },
    } : null);
  }
  const mapping = discoveredWireModelsByBaseUrl.get(scopedKey);
  if (!mapping) return undefined;
  if (mapping.generation
    && !isModelCacheGenerationCurrent(mapping.generation.provider, mapping.generation.cacheGeneration)) {
    // Keep a tombstone: clearing account/cache authority must not reload old disk evidence.
    discoveredWireModelsByBaseUrl.set(scopedKey, null);
    return undefined;
  }
  return mapping;
}

function discoveredAntigravityWireModelId(
  modelId: string,
  baseUrl: string | undefined,
): string | undefined {
  return discoveredAntigravityMapping(baseUrl)?.models.get(modelId);
}

function discoveredAntigravityEffortWireModelId(
  modelId: string,
  effort: string | undefined,
  baseUrl: string | undefined,
): string | undefined {
  const effortMap = discoveredAntigravityMapping(baseUrl)?.effortModels.get(modelId);
  if (!effortMap) return undefined;

  const requestedEffort = effort && ANTIGRAVITY_ABOVE_HIGH_EFFORTS.has(effort) && effortMap.xhigh ? "xhigh" : effort ? resolveAntigravityThinkingLevel(effort) : undefined;
  if (requestedEffort && isAntigravityDiscoveryEffort(requestedEffort) && effortMap[requestedEffort]) {
    return effortMap[requestedEffort];
  }

  const defaultEffort = ANTIGRAVITY_DEFAULT_EFFORT[modelId]
    ?? ANTIGRAVITY_THINKING_LEVEL_MODELS[modelId]
    ?? discoveredAntigravityDefaultEffort(modelId, !!effortMap.medium);
  if (defaultEffort && isAntigravityDiscoveryEffort(defaultEffort) && effortMap[defaultEffort]) {
    return effortMap[defaultEffort];
  }
  return Object.values(effortMap)[0];
}

/**
 * Convert the CCA display label used by `agy` into its public model selector.
 *
 * The wire id is authoritative for requests, while the label is authoritative for the
 * user-facing selector when Google has renamed or re-tiered a model. Keep both instead
 * of maintaining a provider-specific list of known model names.
 */
function antigravityDisplayModelId(displayName: unknown, wireId: string): string | undefined {
  if (typeof displayName !== "string") return undefined;
  const label = displayName.trim();
  if (!label || label.length > 512) return undefined;
  const slug = (replaceDots: boolean): string => label
    .normalize("NFKC")
    .toLowerCase()
    .replace(replaceDots ? /\./g : /\s+/g, replaceDots ? "-" : " ")
    .replace(/[^a-z0-9.-]+/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-|-$/g, "");
  const preserved = slug(false);
  const compact = slug(true);
  if (!isValidModelDiscoveryModelId(preserved) && !isValidModelDiscoveryModelId(compact)) return undefined;
  if (preserved === wireId || compact === wireId
    || preserved === `${wireId}-thinking` || compact === `${wireId}-thinking`) {
    return wireId;
  }
  return isValidModelDiscoveryModelId(preserved) ? preserved : compact;
}

/**
 * Extract the CCA models that are valid for agent requests. The endpoint also returns tab,
 * command, commit-message, transcription, and standalone image-generation models; those are not
 * callable through the CCA agent envelope and must not be published to the Codex catalog.
 */
export function parseAntigravityAvailableModels(
  payload: unknown,
  maxModels = MODEL_DISCOVERY_MAX_MODELS,
): AntigravityAvailableModel[] | null {
  const limit = Number.isSafeInteger(maxModels) && maxModels > 0
    ? Math.min(maxModels, MODEL_DISCOVERY_MAX_MODELS)
    : MODEL_DISCOVERY_MAX_MODELS;
  const body = antigravityRecord(payload);
  if (!body) return null;
  const models = antigravityRecord(body?.models);
  const sorts = Array.isArray(body?.agentModelSorts) ? body.agentModelSorts : undefined;
  if (!models || !sorts) return null;

  const ids: string[] = [];
  // Disabled and duplicate ids cannot consume the enabled-model budget, but raw work is bounded.
  const rawLimit = MODEL_DISCOVERY_MAX_MODELS * 4;
  let rawCount = 0;
  for (const sort of sorts) {
    const groups = antigravityRecord(sort)?.groups;
    if (!Array.isArray(groups)) return null;
    for (const group of groups) {
      const modelIds = antigravityRecord(group)?.modelIds;
      if (!Array.isArray(modelIds)) return null;
      for (const id of modelIds) {
        if (!isValidModelDiscoveryModelId(id)
          || !Object.hasOwn(models, id)
          || !antigravityRecord(models[id])
          || ++rawCount > rawLimit) return null;
        ids.push(id);
      }
    }
  }
  // This model is exposed by Antigravity's agent chat surface even though the discovery
  // response groups it under image generation, so it never appears in agentModelSorts.
  if (Array.isArray(body.imageGenerationModelIds)
    && body.imageGenerationModelIds.includes("gemini-3.1-flash-image")
    && Object.hasOwn(models, "gemini-3.1-flash-image")
    && antigravityRecord(models["gemini-3.1-flash-image"])
    && !ids.includes("gemini-3.1-flash-image")) {
    if (++rawCount > rawLimit) return null;
    ids.push("gemini-3.1-flash-image");
  }
  // Newer CCA responses identify tiered Flash models through this index instead of
  // adding their synthetic wire ids to agentModelSorts.
  const tieredModelIds = antigravityRecord(body.tieredModelIds);
  const flashTieredIds = tieredModelIds?.flash;
  if (Array.isArray(flashTieredIds)) {
    for (const id of flashTieredIds) {
      if (!isValidModelDiscoveryModelId(id)
        || !Object.hasOwn(models, id)
        || !antigravityRecord(models[id])
        || ++rawCount > rawLimit) return null;
      const baseId = id.endsWith("-tiered") ? id.slice(0, -"-tiered".length) : id;
      if (ids.some(agentId =>
        agentId === id
        || agentId === baseId
        || ANTIGRAVITY_DISCOVERY_EFFORTS.some(effort => agentId === `${baseId}-${effort}`)
      )) continue;
      ids.push(id);
    }
  }

  const available = new Map<string, Record<string, unknown>>();
  for (const wireId of ids) {
    const info = antigravityRecord(models[wireId]);
    if (!info || info.disabled === true || wireId.endsWith("-disabled") || available.has(wireId)) continue;
    if (available.size >= limit) return null;
    // Compatibility aliases are deliberately routed to NEWER wire ids for saved
    // selections. CCA keeps serving retired generations (3.5/3.6 Flash tiers) in its
    // agent list long after they stop being the model anyone should pick, so admitting
    // them as independently discovered rows republishes exactly the dead tiers the
    // alias map exists to retire. Routing for a saved id still works — it resolves
    // through ANTIGRAVITY_MODEL_ALIASES — it just no longer gets its own picker row.
    const alias = Object.hasOwn(ANTIGRAVITY_MODEL_ALIASES, wireId)
      ? ANTIGRAVITY_MODEL_ALIASES[wireId]
      : undefined;
    if (alias && alias !== wireId) continue;
    available.set(wireId, info);
  }

  const out: AntigravityAvailableModel[] = [];
  const seen = new Set<string>();
  for (const [wireId, info] of available) {
    const id = pickerModelIdForDiscoveredWireId(wireId, info, available);
    if (seen.has(id)) continue;
    seen.add(id);
    const effortWireModelIds = completeDiscoveredEffortWireModelIds(id, available);
    const tierInfo = effortWireModelIds
      ? Object.values(effortWireModelIds).map(wire => available.get(wire)!) : [info];
    const windows = tierInfo.map(tier => antigravityPositiveInteger(tier.maxTokens));
    const contextWindow = windows.every((window): window is number => window !== undefined)
      ? Math.min(...windows) : undefined;
    const supportsImages = tierInfo.every(tier => tier.supportsImages === true) ? true
      : tierInfo.some(tier => tier.supportsImages === false) ? false : undefined;
    out.push({
      id,
      wireModelId: wireId,
      ...(effortWireModelIds ? { effortWireModelIds } : {}),
      ...(contextWindow ? { contextWindow } : {}),
      // Tri-state, deliberately not a ternary: `true` asserts image support,
      // `false` asserts against it, and ABSENT is unknown. Collapsing absent into
      // `["text"]` let routing read it as a confident `image: false` (#1796). The
      // strict catalog still receives its `["text"]` compatibility default
      // downstream via ensureStrictCatalogFields; only the routing-evidence
      // channel stays honest about what was never asserted.
      ...(supportsImages === true
        ? { inputModalities: ["text", "image"] as string[] }
        : supportsImages === false
          ? { inputModalities: ["text"] as string[] }
          : {}),
    });
  }
  return out;
}

export function resolveAntigravityWireModelId(modelId: string, baseUrl?: string): string {
  const discovered = discoveredAntigravityWireModelId(modelId, baseUrl);
  if (discovered) return discovered;
  return Object.hasOwn(ANTIGRAVITY_MODEL_ALIASES, modelId)
    ? ANTIGRAVITY_MODEL_ALIASES[modelId]
    : modelId;
}

/**
 * Whether the given model ID is a suffix wire ID or compat alias that already encodes
 * an effort level. The suffix names the effort; an opted-in Argon summary additionally
 * repeats that same level in thinkingConfig.
 */
export function isAntigravitySuffixModelId(modelId: string): boolean {
  return !ANTIGRAVITY_KNOWN_PICKER_MODELS.includes(modelId);
}

/** The reasoning tier a retired Flash id used to encode, if it is one. */
export function retiredAntigravityFlashTier(modelId: string): string | undefined {
  return Object.hasOwn(RETIRED_FLASH_TIERS, modelId) ? RETIRED_FLASH_TIERS[modelId] : undefined;
}

/**
 * Resolve a picker-visible base model + optional reasoning effort to the CCA wire model ID.
 *
 * Precedence (evaluated in order):
 * 1. Suffix wire ID or compat alias → resolve via `resolveAntigravityWireModelId`.
 * 2. Mapped Gemini base with effort → return mapped wire ID + thinkingLevel.
 * 3. Mapped Gemini base without effort → return default-effort wire ID.
 * 4. Claude Opus with effort → return identity + thinkingLevel (no suffix variants exist).
 * 5. All other IDs → return `resolveAntigravityWireModelId(modelId)`, no thinkingConfig.
 */
export function resolveAntigravityEffortWireModel(
  modelId: string,
  effort?: string,
  baseUrl?: string,
  options?: { includeThoughts?: boolean },
): { wireModelId: string; thinkingLevel?: string } {
  const discoveredEffortWireModelId = discoveredAntigravityEffortWireModelId(modelId, effort, baseUrl);
  if (discoveredEffortWireModelId) {
    return suffixThoughtWire(discoveredEffortWireModelId, options?.includeThoughts);
  }

  // A collapsed picker row reports ONE representative wire id (whichever tier CCA
  // listed first), so live discovery cannot describe a ladder — it can only name a
  // single rung. Letting it answer for a base model we already have a ladder for
  // collapses every effort onto that one rung: `gemini-3.1-pro` low and high both
  // became `gemini-pro-agent`, and `gemini-3.7-flash` sent thinkingLevel=low against
  // the `-high` wire id, a request that contradicts itself. Rules 1b/2/3 below own
  // these models; discovery answers only for ids no rule knows.
  const hasOwnEffortLadder = Object.hasOwn(ANTIGRAVITY_THINKING_LEVEL_MODELS, modelId)
    || Object.hasOwn(ANTIGRAVITY_EFFORT_WIRE_MAP, modelId);
  const discoveredWireModelId = hasOwnEffortLadder
    ? undefined
    : discoveredAntigravityWireModelId(modelId, baseUrl);
  if (discoveredWireModelId && (discoveredWireModelId !== modelId || isAntigravitySuffixModelId(modelId))) {
    const defaultLevel = ANTIGRAVITY_THINKING_LEVEL_MODELS[modelId];
    const suffixLevel = suffixThoughtLevel(discoveredWireModelId, options?.includeThoughts);
    return {
      wireModelId: discoveredWireModelId,
      ...(defaultLevel
        ? { thinkingLevel: effort ? resolveAntigravityThinkingLevel(effort) ?? defaultLevel : defaultLevel }
        : suffixLevel ? { thinkingLevel: suffixLevel } : {}),
    };
  }

  // Rule 0: retired Flash id — Google has taken the wire id offline, so route to the 3.7
  // redirect target and carry the tier the retired id encoded. (3.7, not "the current
  // generation": 3.8 is current but these ids were retired onto 3.7, which is still served.)
  // This runs BEFORE the suffix check because those ids are aliases, and rule 1 would drop
  // the tier.
  const retiredTier = retiredAntigravityFlashTier(modelId);
  if (retiredTier) {
    return {
      wireModelId: GEMINI_RETIRED_FLASH_TARGET_WIRE_ID,
      thinkingLevel: effort ? resolveAntigravityThinkingLevel(effort) ?? retiredTier : retiredTier,
    };
  }

  // Rule 1: suffix/compat alias — suffix IS the effort.
  if (isAntigravitySuffixModelId(modelId)) {
    const wireModelId = resolveAntigravityWireModelId(modelId, baseUrl);
    return suffixThoughtWire(wireModelId, options?.includeThoughts);
  }

  // Rule 1b: single-wire-id Gemini model whose tiers ride on thinkingLevel. Without
  // this the model falls to rule 5 and silently loses reasoning control entirely.
  const defaultLevel = ANTIGRAVITY_THINKING_LEVEL_MODELS[modelId];
  if (defaultLevel) {
    return {
      wireModelId: pickerToWireId(modelId),
      thinkingLevel: effort ? resolveAntigravityThinkingLevel(effort) ?? defaultLevel : defaultLevel,
    };
  }

  // Rule 2/3: mapped Gemini base model.
  const effortMap = ANTIGRAVITY_EFFORT_WIRE_MAP[modelId];
  if (effortMap) {
    const suffixTiered = ANTIGRAVITY_SUFFIX_TIER_MODELS.has(modelId);
    // Normalize FIRST for suffix-tiered models. The discovery path clamps max/xhigh/ultra to
    // `high` for a family without a discovered xhigh tier (a family that has one selects it, see
    // ANTIGRAVITY_ABOVE_HIGH_EFFORTS), so a static path that skipped the clamp answered `medium`
    // for the same request: one input, two tiers, decided by whether discovery happened to run.
    const requested = suffixTiered && effort
      ? resolveAntigravityThinkingLevel(effort) ?? effort
      : effort;
    if (requested && Object.hasOwn(effortMap, requested)) {
      const wireModelId = effortMap[requested]!;
      return suffixTiered ? suffixThoughtWire(wireModelId, options?.includeThoughts)
        : { wireModelId, thinkingLevel: requested };
    }
    const defaultEffort = ANTIGRAVITY_DEFAULT_EFFORT[modelId]!;
    return suffixThoughtWire(effortMap[defaultEffort]!, options?.includeThoughts);
  }

  // Rule 4: Claude models — effort via thinkingConfig only (no suffix variants).
  // CCA validates this field as Google's ThinkingLevel enum, whose highest value is `high`.
  if (/^claude-/.test(modelId) && effort) {
    return { wireModelId: modelId, thinkingLevel: resolveAntigravityThinkingLevel(effort) };
  }

  // Rule 5: everything else.
  return { wireModelId: resolveAntigravityWireModelId(modelId, baseUrl) };
}


// ── Usage aggregation reverse map (picker/call base identity) ──
// Effort wire IDs and compatibility aliases must collapse to the same picker-visible
// base model that users invoke after effort routing. Historical logs store suffix IDs;
// summary aggregation consults this reverse map so one call model = one usage row.
const ANTIGRAVITY_USAGE_BASE_BY_ID: Record<string, string> = (() => {
  const rev: Record<string, string> = {};
  for (const base of ANTIGRAVITY_KNOWN_PICKER_MODELS) rev[base] = base;
  for (const [base, effortMap] of Object.entries(ANTIGRAVITY_EFFORT_WIRE_MAP)) {
    rev[base] = base;
    for (const wire of Object.values(effortMap)) rev[wire] = base;
  }
  for (const [alias, wire] of Object.entries(ANTIGRAVITY_MODEL_ALIASES)) {
    const base = rev[wire] ?? rev[alias];
    if (base) rev[alias] = base;
    // If alias is itself a base/wire already mapped, keep that mapping.
    else if (rev[wire]) rev[alias] = rev[wire]!;
  }
  // Retired ids keep their OWN identity for usage aggregation, overriding the alias
  // pass above. Routing sends new 3.6 calls to 3.7, but a usage row written months ago
  // records a model the user actually called: relabelling it would move historical spend
  // onto a model that did not exist then, and away from the 3.6 price row that still
  // prices it correctly. Retirement changes what we CALL, not what we RECORD.
  for (const retired of Object.keys(RETIRED_FLASH_TIERS)) rev[retired] = retired;
  // Usage identity is stable even before discovery; routing keeps its own live evidence.
  for (const base of ["claude-sonnet-5-5", "claude-opus-5-5"]) {
    rev[base] = base;
    for (const effort of ANTIGRAVITY_DISCOVERY_EFFORTS) rev[`${base}-${effort}`] = base;
  }
  // Visible aliases that only appear in ANTIGRAVITY_VISIBLE_MODEL_ALIASES are already
  // included via ANTIGRAVITY_MODEL_ALIASES. Identity bases without effort maps remain.
  return rev;
})();

/**
 * Canonical picker/call model id for usage aggregation.
 * Unknown ids stay identity so future CCA models do not invent mappings.
 */
export function canonicalAntigravityUsageModel(modelId: string): string {
  const id = modelId.trim();
  if (!id) return modelId;
  return ANTIGRAVITY_USAGE_BASE_BY_ID[id] ?? id;
}
