/**
 * Gemini model pricing map for LLM cost tracking.
 *
 * Prices are in USD per 1 million tokens (prompt and completion separately).
 * Source: Google AI Studio / Gemini pricing page (publicly documented rates).
 *
 * Keep this map in sync with any new models added to the system.
 * If a model is not listed here, computeCostUsd returns null (no silent zeros).
 *
 * Last updated: 2026-06-03
 * Source: https://ai.google.dev/gemini-api/docs/models — verify rates when adding new models.
 */

export interface ModelPricing {
  /** USD cost per 1 million prompt tokens. */
  promptUsdPerMillion: number;
  /** USD cost per 1 million completion (candidate) tokens. */
  completionUsdPerMillion: number;
}

/**
 * Pricing map keyed by exact Gemini model ID string.
 * The model ID must match what is stored in llm_jobs.model_name verbatim.
 */
export const GEMINI_PRICING: Record<string, ModelPricing> = {
  // ── Gemini 2.5 series ──────────────────────────────────────────────────────
  // Source: https://ai.google.dev/gemini-api/docs/models (Apr 2026)
  'gemini-2.5-pro': {
    promptUsdPerMillion: 1.25,
    completionUsdPerMillion: 5.0,
  },
  'gemini-2.5-pro-preview-05-06': {
    promptUsdPerMillion: 1.25,
    completionUsdPerMillion: 10.0,
  },
  'gemini-2.5-flash': {
    promptUsdPerMillion: 0.15,
    completionUsdPerMillion: 0.6,
  },
  'gemini-2.5-flash-preview-04-17': {
    promptUsdPerMillion: 0.15,
    completionUsdPerMillion: 0.6,
  },

  // ── Gemini 2.0 series ──────────────────────────────────────────────────────
  'gemini-2.0-flash': {
    promptUsdPerMillion: 0.1,
    completionUsdPerMillion: 0.4,
  },
  'gemini-2.0-flash-001': {
    promptUsdPerMillion: 0.1,
    completionUsdPerMillion: 0.4,
  },
  'gemini-2.0-flash-lite': {
    promptUsdPerMillion: 0.075,
    completionUsdPerMillion: 0.3,
  },
  'gemini-2.0-flash-lite-001': {
    promptUsdPerMillion: 0.075,
    completionUsdPerMillion: 0.3,
  },
  'gemini-2.0-flash-preview': {
    promptUsdPerMillion: 0.1,
    completionUsdPerMillion: 0.4,
  },

  // ── Gemini 1.5 series (legacy, kept for historical job rows) ───────────────
  'gemini-1.5-pro': {
    promptUsdPerMillion: 1.25,
    completionUsdPerMillion: 5.0,
  },
  'gemini-1.5-flash': {
    promptUsdPerMillion: 0.075,
    completionUsdPerMillion: 0.3,
  },
  'gemini-1.5-flash-8b': {
    promptUsdPerMillion: 0.0375,
    completionUsdPerMillion: 0.15,
  },

  // ── Gemini 3.x series ─────────────────────────────────────────────────────
  // Pricing not yet published as of 2026-06-03; estimated same tier as 2.0-flash-lite.
  // Verify at: https://ai.google.dev/gemini-api/docs/models when official rates land.
  'gemini-3.1-flash-lite': {
    promptUsdPerMillion: 0.075,
    completionUsdPerMillion: 0.3,
  },
  // Estimated same tier as 2.0-flash. Update when pricing is published.
  // Verify at: https://ai.google.dev/gemini-api/docs/models
  'gemini-3-flash-preview': {
    promptUsdPerMillion: 0.1,
    completionUsdPerMillion: 0.4,
  },
};

/**
 * Compute the USD cost for a single LLM call.
 *
 * @param modelName        - Exact model ID string (as stored in llm_jobs.model_name).
 * @param promptTokens     - Number of prompt tokens consumed.
 * @param completionTokens - Number of completion tokens generated.
 * @returns Cost in USD, or null if the model is not in the pricing map.
 */
export function computeCostUsd(
  modelName: string,
  promptTokens: number,
  completionTokens: number,
): number | null {
  const pricing = GEMINI_PRICING[modelName];
  if (!pricing) return null;

  const promptCost = (promptTokens / 1_000_000) * pricing.promptUsdPerMillion;
  const completionCost = (completionTokens / 1_000_000) * pricing.completionUsdPerMillion;
  return promptCost + completionCost;
}
