import bcrypt from 'bcryptjs';

const DEFAULT_ROUNDS = 12;

export function getSaltRounds(): number {
  const val = Number(process.env.BCRYPT_ROUNDS ?? DEFAULT_ROUNDS);
  return Number.isInteger(val) && val > 8 ? val : DEFAULT_ROUNDS;
}

export async function hashPassword(password: string): Promise<string> {
  return bcrypt.hash(password, getSaltRounds());
}

export async function verifyPassword(password: string, hash: string): Promise<boolean> {
  if (!hash) return false;
  return bcrypt.compare(password, hash);
}
