import { deriveSessionTotalTokens, type NormalizedUsage } from "../../agents/usage.js";
import { incrementCompactionCount } from "./session-updates.js";
import { persistSessionUsageUpdate } from "./session-usage.js";

type PersistRunSessionUsageParams = Parameters<typeof persistSessionUsageUpdate>[0];

type IncrementRunCompactionCountParams = Omit<
  Parameters<typeof incrementCompactionCount>[0],
  "tokensAfter"
> & {
  lastCallUsage?: NormalizedUsage;
  contextTokensUsed?: number;
};

export async function persistRunSessionUsage(params: PersistRunSessionUsageParams): Promise<void> {
  await persistSessionUsageUpdate({
    storePath: params.storePath,
    sessionKey: params.sessionKey,
    usage: params.usage,
    lastCallUsage: params.lastCallUsage,
    promptTokens: params.promptTokens,
    modelUsed: params.modelUsed,
    providerUsed: params.providerUsed,
    contextTokensUsed: params.contextTokensUsed,
    systemPromptReport: params.systemPromptReport,
    cliSessionId: params.cliSessionId,
    logLabel: params.logLabel,
  });
}

export async function incrementRunCompactionCount(
  params: IncrementRunCompactionCountParams,
): Promise<number | undefined> {
  const tokensAfterCompaction = params.lastCallUsage
    ? deriveSessionTotalTokens({
        usage: params.lastCallUsage,
        contextTokens: params.contextTokensUsed,
      })
    : undefined;
  return incrementCompactionCount({
    sessionEntry: params.sessionEntry,
    sessionStore: params.sessionStore,
    sessionKey: params.sessionKey,
    storePath: params.storePath,
    tokensAfter: tokensAfterCompaction,
  });
}
