import { randomInt } from 'node:crypto';

/**
 * Temporary passwords for new venue owners/hosts and password resets. Shown once to the admin,
 * stored only as a scrypt hash. The alphabet drops look-alike characters (0/O, 1/l/I) because the
 * password is read aloud or copied by hand; 16 characters of 55 symbols ≈ 92 bits of entropy.
 */
const ALPHABET = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';

export const TEMP_PASSWORD_LENGTH = 16;

/** e.g. "k7Qm-Rt9x-Hc2w-Pn4z": groups of four for readability (hyphens are part of the password). */
export function generateTempPassword(length: number = TEMP_PASSWORD_LENGTH): string {
  if (!Number.isInteger(length) || length < 12 || length > 64) throw new RangeError('length must be 12-64');
  let raw = '';
  for (let i = 0; i < length; i++) raw += ALPHABET[randomInt(ALPHABET.length)];
  return raw.match(/.{1,4}/g)!.join('-');
}
