// utils/otpUtils.js
import crypto from 'crypto';

// In-memory store for OTPs (use a database like Redis for production)
const otpStore = new Map();

const generateOtp = (email) => {
  const otp = crypto.randomInt(100000, 1000000).toString(); // Generate 6-digit OTP
  const expiresIn = Date.now() + 5 * 60 * 1000; // Valid for 5 minutes

  otpStore.set(email, { otp, expiresIn });
  return otp;
};

const verifyOtp = (email, otp) => {
  const data = otpStore.get(email);
  if (!data) return false;

  const { otp: storedOtp, expiresIn } = data;

  if (otp === storedOtp && Date.now() < expiresIn) {
    otpStore.delete(email); // OTP is one-time use
    return true;
  }
  return false;
};

export { generateOtp, verifyOtp };