/**
 * cost/pricing.mjs — runtime-callable pricing table + calculateCost (C11).
 *
 * Mirror of cost/pricing.ts kept in .mjs so the CLI dispatcher (which runs
 * uncompiled .mjs source) can import without a TypeScript build step. The
 * TS file is the source of truth for the exported library surface; this
 * file is the source of truth for the CLI runtime. Keep the table values
 * identical between the two; the cost-tracker.test.ts asserts the TS table
 * shape, so any drift in the .ts file will immediately cost-trip there.
 */

/** USD per million tokens. */
export const DEFAULT_PRICING = Object.freeze({
	'claude-opus-4-6': { inputPerMillion: 15, outputPerMillion: 75 },
	'claude-sonnet-4-5-20250929': { inputPerMillion: 3, outputPerMillion: 15 },
	'claude-sonnet-4-20250514': { inputPerMillion: 3, outputPerMillion: 15 },
	'claude-haiku-3-5-20241022': { inputPerMillion: 0.8, outputPerMillion: 4 },
	'gpt-4o': { inputPerMillion: 2.5, outputPerMillion: 10 },
	'gpt-4o-mini': { inputPerMillion: 0.15, outputPerMillion: 0.6 },
	'o3-mini': { inputPerMillion: 1.1, outputPerMillion: 4.4 },
	o3: { inputPerMillion: 10, outputPerMillion: 40 },
	'gemini-2.5-pro': { inputPerMillion: 1.25, outputPerMillion: 10 },
	'gemini-2.5-flash': { inputPerMillion: 0.15, outputPerMillion: 0.6 },
})

/**
 * Calculate cost in USD for a given token usage and model.
 * Returns { costUsd, pricingSource: 'known' | 'custom' | 'unknown' }.
 */
export function calculateCost(inputTokens, outputTokens, modelId, customPricing) {
	if (customPricing && Object.hasOwn(customPricing, modelId)) {
		const p = customPricing[modelId]
		return {
			costUsd:
				(inputTokens / 1_000_000) * p.inputPerMillion +
				(outputTokens / 1_000_000) * p.outputPerMillion,
			pricingSource: 'custom',
		}
	}
	if (Object.hasOwn(DEFAULT_PRICING, modelId)) {
		const p = DEFAULT_PRICING[modelId]
		return {
			costUsd:
				(inputTokens / 1_000_000) * p.inputPerMillion +
				(outputTokens / 1_000_000) * p.outputPerMillion,
			pricingSource: 'known',
		}
	}
	return { costUsd: 0, pricingSource: 'unknown' }
}
