/**
 * Model cost table (USD per 1K tokens) — single source of truth.
 *
 * Gunakan calcEstimatedCost() untuk menghitung biaya dari token usage.
 * Jangan duplikat tabel ini di route files — import dari sini.
 *
 * Format key:
 *   - Provider langsung: "model-id" (misal: "gpt-4o-mini")
 *   - AgentRouter gateway: "agentrouter/model-id"
 */

export interface TokenCost {
  input:  number; // USD per 1K prompt tokens
  output: number; // USD per 1K completion tokens
}

export const MODEL_COST_PER_1K: Record<string, TokenCost> = {
  // ── OpenAI (direct) ────────────────────────────────────────────────────────
  "gpt-4o-mini":                { input: 0.00015,  output: 0.0006  },
  "gpt-4o":                     { input: 0.005,    output: 0.015   },
  "gpt-4o-2024-11-20":          { input: 0.0025,   output: 0.01    },

  // ── Anthropic (direct) ────────────────────────────────────────────────────
  "claude-3-haiku-20240307":    { input: 0.00025,  output: 0.00125 },
  "claude-3-5-haiku-20241022":  { input: 0.0008,   output: 0.004   },
  "claude-3-5-sonnet-20240620": { input: 0.003,    output: 0.015   },
  "claude-3-5-sonnet-20241022": { input: 0.003,    output: 0.015   },

  // ── Google (direct) ───────────────────────────────────────────────────────
  "gemini-1.5-flash":           { input: 0.000075, output: 0.0003  },
  "gemini-1.5-pro":             { input: 0.00125,  output: 0.005   },
  "gemini-2.0-flash":           { input: 0.0001,   output: 0.0004  },
  "gemini-2.5-flash":           { input: 0.00015,  output: 0.0006  },

  // ── AgentRouter (gateway) — key: "agentrouter/model-id" ─────────────────
  "agentrouter/claude-3-haiku-20240307":    { input: 0.00022,  output: 0.0011  },
  "agentrouter/claude-3-5-haiku-20241022":  { input: 0.0007,   output: 0.0035  },
  "agentrouter/claude-3-5-sonnet-20240620": { input: 0.0028,   output: 0.014   },
  "agentrouter/claude-3-5-sonnet-20241022": { input: 0.0028,   output: 0.014   },
  "agentrouter/gpt-4o-mini":                { input: 0.00014,  output: 0.00055 },
  "agentrouter/gpt-4o":                     { input: 0.0045,   output: 0.014   },
  "agentrouter/gemini-1.5-flash":           { input: 0.00007,  output: 0.00028 },
  "agentrouter/gemini-2.0-flash":           { input: 0.000095, output: 0.00038 },
  "agentrouter/deepseek-chat":              { input: 0.00027,  output: 0.0011  },
  "agentrouter/deepseek-reasoner":          { input: 0.00055,  output: 0.00219 },
};

/** Fallback cost untuk model yang tidak dikenal */
const FALLBACK_COST: TokenCost = { input: 0.001, output: 0.002 };

/**
 * Hitung estimasi biaya dari token usage.
 * @param provider  - nama provider ("openai", "anthropic", "google", "agentrouter")
 * @param model     - nama model (misal: "gpt-4o-mini")
 * @param usage     - { promptTokens, completionTokens }
 * @returns         - estimasi biaya dalam USD, atau null jika usage tidak tersedia
 */
export function calcEstimatedCost(
  provider: string,
  model: string,
  usage?: { promptTokens: number; completionTokens: number } | null
): number | null {
  if (!usage) return null;
  const key   = provider === "agentrouter" ? `agentrouter/${model}` : model;
  const costs = MODEL_COST_PER_1K[key] ?? FALLBACK_COST;
  return (
    (usage.promptTokens     / 1000) * costs.input +
    (usage.completionTokens / 1000) * costs.output
  );
}
