// Shared client export model metadata.
import { getModelMetadata, resolveMetadataProvider } from "../../generated/model-metadata";
import { SCHEMA_REQUIRED_OUTPUT_BUDGET } from "./constants";
import { expandFastExportModels } from "./fast-models";
import { sanitizeCodexReasoningEfforts } from "../../reasoning-effort";
import type { OpencodeCatalogModel, ExportModel, ExportClientId, ManagedContribution } from "./contracts";
import type { OcxConfig } from "../../types";
import { shouldInjectApiAuthHeader } from "../../codex/inject";


/**
 * Validate the supplied catalog window (which may be a labelled vendor estimate).
 * A missing, non-finite, or non-positive value omits every context-derived field;
 * this serializer does not independently infer a window.
 */
export function authoritativeContextWindow(contextWindow: number | undefined): number | undefined {
  if (typeof contextWindow === "number" && Number.isFinite(contextWindow) && contextWindow > 0) {
    const integer = Math.floor(contextWindow);
    return integer > 0 ? integer : undefined;
  }
  return undefined;
}

/** Known model output limit, with a schema-required fallback, clamped to context. */
export function outputBudgetFor(context: number, model: OpencodeCatalogModel): number {
  const provider = model.provider ?? "";
  const metadata = getModelMetadata(resolveMetadataProvider(provider) ?? provider, model.id ?? model.namespaced);
  const limit = authoritativeContextWindow(model.maxTokens)
    ?? authoritativeContextWindow(metadata?.maxTokens)
    ?? SCHEMA_REQUIRED_OUTPUT_BUDGET;
  return Math.min(limit, context);
}

/**
* Known model output limit without the schema stand-in, clamped to context.
*
* `outputBudgetFor` always returns a number because the clients that require the field
* cannot express "unknown". A client that instead treats an absent `maxTokens` as a real
* default must not be handed the 32000 stand-in: that would claim a limit no model declared
* (#5828). Use this variant where "no claim" is representable.
* @param context - the context window this model already exports.
* @param model - the catalog row, which may carry an authoritative output limit.
* @returns the known limit clamped to `context`, or undefined when nothing is known.
*/
export function knownOutputBudget(context: number, model: OpencodeCatalogModel): number | undefined {
  const provider = model.provider ?? "";
  const metadata = getModelMetadata(resolveMetadataProvider(provider) ?? provider, model.id ?? model.namespaced);
  const limit = authoritativeContextWindow(model.maxTokens)
    ?? authoritativeContextWindow(metadata?.maxTokens);
  return limit === undefined ? undefined : Math.min(limit, context);
}

/** Input limits are optional: unlike output, a missing limit needs no schema fallback. */
export function inputBudgetFor(context: number, model: OpencodeCatalogModel): number | undefined {
  const limit = authoritativeContextWindow(model.maxInputTokens);
  return limit === undefined ? undefined : Math.min(limit, context);
}

/**
 * Modalities a given client's schema will actually accept.
 *
 * Our internal vocabulary is `text | image | audio` (ALLOWED_INPUT_MODALITIES in
 * src/server/management/model-routes.ts). Pi and Gajae accept only
 * `text | image`, and both reject the WHOLE config file over one out-of-enum
 * value — Gajae reports `/providers/opencodex/models/N/input/2: Invalid option`
 * and falls back to its built-in list, Pi returns an empty model config. So a
 * single `audio` model takes every routed model down with it. That is not
 * hypothetical: zenmux/meta-muse-spark-1.1 advertises audio and did exactly
 * this. It is also the same defect the Codex catalog had with `video`, where
 * the app showed zero apps (tests/codex-integration/catalog-input-modality-enum.test.ts).
 *
 * UNKNOWN and INCOMPATIBLE are different inputs, and the Codex fix could
 * conflate them safely only because its enum is wider. A model with nothing
 * declared is unknown, and `text` is the honest floor — every routed model takes
 * prompts. A model declaring `["audio"]` and nothing else is incompatible with a
 * text|image client, and rewriting it to `["text"]` would advertise a capability
 * it does not have. That input is reachable three ways: `ocx models add
 * --modalities audio`, `/api/custom-models`, and provider discovery.
 *
 * So unknown falls back to text and incompatible returns null, which drops the
 * row. Omitting a model costs the user a line in a picker; fabricating `text`
 * costs them a model that fails at call time with no explanation.
 *
 * Deliberately NOT applied in `ExportModel` construction: the management and CLI
 * boundaries carry catalog modalities verbatim on purpose, and stripping `audio`
 * globally would destroy valid metadata before the destination is known.
 */
const CLIENT_INPUT_MODALITIES: Record<"pi" | "gajae", ReadonlySet<string>> = {
  pi: new Set(["text", "image"]),
  gajae: new Set(["text", "image"]),
};

/** `null` means the model cannot be represented for this client — drop the row. */
export function inputModalitiesForClient(
  client: "pi" | "gajae",
  modalities: readonly string[] | undefined,
): string[] | null {
  const declared = modalities ?? [];
  if (declared.length === 0) return ["text"];
  const accepted = CLIENT_INPUT_MODALITIES[client];
  const kept: string[] = [];
  for (const value of declared) {
    if (accepted.has(value) && !kept.includes(value)) kept.push(value);
  }
  return kept.length > 0 ? kept : null;
}

/**
 * Effort ladder a ZCode surface can offer as picker values, or undefined when the row
 * has nothing selectable.
 *
 * Both ZCode exports — the legacy `v2/config.json` block's `reasoning.variants` and the
 * `v2/provider_config.json` store's `optionSpecs.reasoningLevel.values` — carry the same
 * Codex vocabulary: the client validates the chosen level against the emitted list and
 * forwards it verbatim, and `/v1/responses` reads that vocabulary natively. `none` is a
 * Codex omit-sentinel, not a picker option, so it never survives into the values.
 */
export function zcodeSelectableEfforts(efforts: readonly string[] | undefined): string[] | undefined {
  const selectable = sanitizeCodexReasoningEfforts(efforts)?.filter(effort => effort !== "none");
  return selectable && selectable.length > 0 ? selectable : undefined;
}

/**
 * Input modalities opencode's model schema accepts (opencode.ai/config.json, both
 * `modalities.input` and `modalities.output`). Wider than our internal `text | image | audio`
 * vocabulary, so unlike Pi and Gajae this filter can only drop a value no current ingress
 * produces: `/api/custom-models`, `ocx models add` and the catalog writer all normalize to
 * the internal three. It exists so a future ingress cannot do to opencode what `audio` did
 * to Gajae, whose loader rejected the whole config file over one out-of-enum value.
 */
const OPENCODE_INPUT_MODALITIES: ReadonlySet<string> = new Set(["text", "audio", "image", "video", "pdf"]);

/**
 * Legacy per-model modality fields for one catalog row, or `undefined` when the row
 * declares nothing.
 *
 * `undefined` rather than guessing `{ input: ["text"] }`: unknown capabilities leave the
 * client's fallback assumptions in place. V2 translates these lists into native capability
 * fields; legacy clients also receive the attachment flag. A declared list is carried across as-is, so
 * an audio-only row keeps `attachment: true` instead of being rewritten to text it cannot
 * read — the same call Pi's exporter makes, in the opposite direction.
 */
export function opencodeModelCapabilities(
  modalities: readonly string[] | undefined,
): { attachment: boolean; modalities: { input: string[]; output: string[] } } | undefined {
  const input: string[] = [];
  for (const value of modalities ?? []) {
    if (OPENCODE_INPUT_MODALITIES.has(value) && !input.includes(value)) input.push(value);
  }
  if (input.length === 0) return undefined;
  // `attachment` is what opencode's client gates pasting on; `modalities` refines it into
  // which kinds. Output is always text — nothing in the catalog declares otherwise.
  return { attachment: input.some(value => value !== "text"), modalities: { input, output: ["text"] } };
}

/**
 * Label shared by every client: `"<displayName|id> (<native|provider|routed>)"`. The
 * provider suffix is what makes two same-named models from different upstreams
 * distinguishable in a client's model picker.
 */
export function exportModelLabel(model: OpencodeCatalogModel): string {
  const providerLabel = model.native ? "native" : (model.provider ?? "routed");
  const id = model.id ?? model.namespaced;
  if (model.displayName && model.displayName.length > 0) {
    return `${model.displayName} (${providerLabel})`;
  }
  return `${id} (${providerLabel})`;
}

/**
 * Shared precondition for every serializer: expand hub-approved Fast rows, drop duplicate
 * `namespaced` (first wins, native rows lead `/api/models`) and sort so calls with the same
 * models produce identical bytes. Stability matters because the GUI shows a diffable
 * preview and agents may checksum the payload.
 */
export function normalizeExportModels(models: readonly ExportModel[]): ExportModel[] {
  return expandFastExportModels(models)
    .sort((a, b) => (a.namespaced < b.namespaced ? -1 : a.namespaced > b.namespaced ? 1 : 0));
}

/** Extra headers a non-loopback bind needs, or nothing on loopback. */
export function proxyAdmissionHeaders(config: OcxConfig | undefined, envRef: string): Record<string, string> | undefined {
  return shouldInjectApiAuthHeader(config) ? { "x-opencodex-api-key": envRef } : undefined;
}

/** One fragment at `path`, built from this client's own document. */
export function singleFragment(clientId: ExportClientId, path: readonly string[], value: unknown): ManagedContribution {
  return { clientId, fragments: [{ path, value }] };
}
