#!/usr/bin/env node

// src/core/pricing.ts
var PRICING_TABLE = {
  // ── Anthropic Claude ──────────────────────────────────────────
  "claude-opus-4-8": { input: 15, output: 75, cacheWrite: 18.75, cacheRead: 1.5 },
  "claude-opus-4-7": { input: 15, output: 75, cacheWrite: 18.75, cacheRead: 1.5 },
  "claude-opus-4-6": { input: 15, output: 75, cacheWrite: 18.75, cacheRead: 1.5 },
  "claude-opus-4-5": { input: 15, output: 75, cacheWrite: 18.75, cacheRead: 1.5 },
  "claude-sonnet-4-6": { input: 3, output: 15, cacheWrite: 3.75, cacheRead: 0.3 },
  "claude-sonnet-4-5-20250929": { input: 3, output: 15, cacheWrite: 3.75, cacheRead: 0.3 },
  "claude-haiku-4-5-20251001": { input: 1, output: 5, cacheWrite: 1.25, cacheRead: 0.1 },
  "claude-haiku-4-5": { input: 1, output: 5, cacheWrite: 1.25, cacheRead: 0.1 },
  // Legacy Claude 3.x families (prefix fallback handles minor date suffixes)
  "claude-3-5-sonnet": { input: 3, output: 15, cacheWrite: 3.75, cacheRead: 0.3 },
  "claude-3-5-haiku": { input: 0.8, output: 4, cacheWrite: 1, cacheRead: 0.08 },
  "claude-3-opus": { input: 15, output: 75, cacheWrite: 18.75, cacheRead: 1.5 },
  // ── OpenAI ────────────────────────────────────────────────────
  "gpt-4o": { input: 2.5, output: 10, cacheRead: 1.25 },
  "gpt-4o-mini": { input: 0.15, output: 0.6, cacheRead: 0.075 },
  "gpt-4-turbo": { input: 10, output: 30 },
  "gpt-4": { input: 30, output: 60 },
  "gpt-4.1": { input: 2, output: 8, cacheRead: 0.5 },
  "gpt-4.1-mini": { input: 0.4, output: 1.6, cacheRead: 0.1 },
  "gpt-4.1-nano": { input: 0.1, output: 0.4, cacheRead: 0.025 },
  "o1": { input: 15, output: 60, cacheRead: 7.5 },
  "o1-mini": { input: 3, output: 12, cacheRead: 1.5 },
  "o3": { input: 10, output: 40, cacheRead: 2.5 },
  "o3-mini": { input: 1.1, output: 4.4, cacheRead: 0.55 },
  // ── Google Gemini ─────────────────────────────────────────────
  "gemini-2.5-pro": { input: 1.25, output: 10 },
  "gemini-2.5-flash": { input: 0.3, output: 2.5 },
  "gemini-2.0-flash": { input: 0.1, output: 0.4 },
  "gemini-1.5-pro": { input: 1.25, output: 5 },
  "gemini-1.5-flash": { input: 0.075, output: 0.3 },
  // ── DeepSeek ──────────────────────────────────────────────────
  // V4 family (2026-04-23+)：1M context, Thinking / Non-Thinking dual-mode, 384K max output
  "deepseek-v4-pro": { input: 1.74, output: 3.48, cacheRead: 0.145 },
  "deepseek-v4-flash": { input: 0.14, output: 0.28, cacheRead: 0.028 },
  // Legacy aliases：retires 2026-07-24 UTC 15:59，官方 route 到 V4 Flash
  "deepseek-chat": { input: 0.14, output: 0.28, cacheRead: 0.028 },
  "deepseek-reasoner": { input: 0.14, output: 0.28, cacheRead: 0.028 },
  "deepseek-v3": { input: 0.27, output: 1.1, cacheRead: 0.07 },
  // ── Moonshot Kimi ─────────────────────────────────────────────
  "moonshot-v1-8k": { input: 0.17, output: 0.17 },
  "moonshot-v1-32k": { input: 0.33, output: 0.33 },
  "moonshot-v1-128k": { input: 0.83, output: 0.83 },
  "kimi-k2": { input: 0.6, output: 2.5 },
  "kimi-latest": { input: 0.6, output: 2.5 },
  // ── Zhipu GLM ─────────────────────────────────────────────────
  "glm-4-plus": { input: 0.7, output: 0.7 },
  "glm-4": { input: 0.14, output: 0.14 },
  "glm-4-flash": { input: 0, output: 0 },
  "glm-4.5": { input: 0.29, output: 1.14 },
  "glm-4.6": { input: 0.6, output: 2.2 },
  "glm-4.6v": { input: 0.6, output: 2.2 },
  "glm-5": { input: 0.85, output: 2.85 },
  "glm-5.1": { input: 0.95, output: 3.15 },
  "glm-5.1-reasoning": { input: 1.4, output: 4.4 },
  "glm-5.1-air": { input: 0.4, output: 1.2 },
  "glm-z1": { input: 0.5, output: 1.5 },
  "glm-z1-air": { input: 0.2, output: 0.6 },
  "glm-z1-flash": { input: 0, output: 0 }
  // ── OpenRouter (pass-through — actual cost depends on underlying model) ──
  // Left empty; callers should resolve via underlying model ID.
  // ── Ollama (local, zero cost) ─────────────────────────────────
  // Handled via provider check below.
};
var FREE_PROVIDERS = /* @__PURE__ */ new Set(["ollama"]);
function getPricing(provider, model) {
  if (FREE_PROVIDERS.has(provider.toLowerCase())) {
    return { input: 0, output: 0 };
  }
  const key = model.toLowerCase();
  if (PRICING_TABLE[key]) return PRICING_TABLE[key];
  const keys = Object.keys(PRICING_TABLE).sort((a, b) => b.length - a.length);
  for (const k of keys) {
    if (key.startsWith(k)) return PRICING_TABLE[k];
  }
  return null;
}
function computeCost(provider, model, usage) {
  const p = getPricing(provider, model);
  if (!p) return null;
  const input = usage.inputTokens * p.input;
  const output = usage.outputTokens * p.output;
  const cacheWrite = (usage.cacheCreationTokens ?? 0) * (p.cacheWrite ?? p.input);
  const cacheRead = (usage.cacheReadTokens ?? 0) * (p.cacheRead ?? p.input);
  return (input + output + cacheWrite + cacheRead) / 1e6;
}
function formatCost(amount) {
  if (amount === 0) return "$0.0000";
  if (amount < 0.01) return `$${amount.toFixed(4)}`;
  if (amount < 1) return `$${amount.toFixed(3)}`;
  return `$${amount.toFixed(2)}`;
}

export {
  getPricing,
  computeCost,
  formatCost
};
