import crypto from "crypto";

function getVerificationTtlHours() {
  const value = Number(process.env.EMAIL_VERIFICATION_TTL_HOURS ?? 24);
  return Number.isFinite(value) && value > 0 ? value : 24;
}

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

  return {
    token,
    tokenHash,
    expires,
  };
}

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