/**
 * Token/cost translation into the `Usage` shape pi records for a tool result.
 * Structurally identical to pi-ai's `Usage`, declared locally so the lib stays
 * importable outside the pi runtime (unit tests, scripts).
 */

export interface PiUsage {
  input: number;
  output: number;
  cacheRead: number;
  cacheWrite: number;
  totalTokens: number;
  cost: {
    input: number;
    output: number;
    cacheRead: number;
    cacheWrite: number;
    total: number;
  };
}

export interface TokenCounts {
  input?: number;
  output?: number;
}

export function toPiUsage(tokens: TokenCounts, costUsd: number | undefined): PiUsage {
  const input = tokens.input ?? 0;
  const output = tokens.output ?? 0;
  const total = costUsd ?? 0;
  return {
    input,
    output,
    cacheRead: 0,
    cacheWrite: 0,
    totalTokens: input + output,
    cost: { input: total, output: 0, cacheRead: 0, cacheWrite: 0, total },
  };
}

/** Jev bills input tokens only; output is free. */
export function inputCostUsd(tokens: TokenCounts, costPerMillionInput: number, costPerMillionOutput: number): number {
  return ((tokens.input ?? 0) * costPerMillionInput + (tokens.output ?? 0) * costPerMillionOutput) / 1_000_000;
}
