const sanitizeSeed = (value: string): string => {
  return value
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '')
    .slice(0, 36);
};

export const createIdempotencyKey = (scope: string, seed?: string): string => {
  const base = sanitizeSeed(scope || 'action') || 'action';
  const seedPart = seed ? sanitizeSeed(seed) : '';
  const timestamp = Date.now().toString(36);
  const random = Math.random().toString(36).slice(2, 10);
  return seedPart
    ? `${base}-${seedPart}-${timestamp}-${random}`
    : `${base}-${timestamp}-${random}`;
};
