import crypto from 'crypto';

export const computeSHA256 = (data: string | Buffer): string => {
  return crypto.createHash('sha256').update(data).digest('hex');
};

export const computeCacheKey = (prompt: string, params: Record<string, unknown>): string => {
  const sortedKeys = Object.keys(params).sort();
  const sortedParams: Record<string, unknown> = {};

  for (const key of sortedKeys) {
    sortedParams[key] = params[key];
  }

  const combined = prompt + JSON.stringify(sortedParams);
  return computeSHA256(combined);
};

export const computeFileHash = (buffer: Buffer): string => {
  return computeSHA256(buffer);
};
