import crypto from "crypto";

function getPasswordResetTtlHours() {
  const value = Number(process.env.PASSWORD_RESET_TTL_HOURS ?? 2);
  return Number.isFinite(value) && value > 0 ? value : 2;
}

export function generatePasswordResetToken() {
  const token = crypto.randomBytes(32).toString("hex");
  const tokenHash = crypto.createHash("sha256").update(token).digest("hex");
  const expiresAt = new Date(Date.now() + getPasswordResetTtlHours() * 60 * 60 * 1000);

  return {
    token,
    tokenHash,
    expiresAt,
  };
}

export function hashPasswordResetToken(token: string) {
  return crypto.createHash("sha256").update(token).digest("hex");
}
