import Redis from 'ioredis'
import dotenv from 'dotenv'
dotenv.config()

const redis = new Redis({
  host: process.env.REDIS_HOST,
  port: process.env.REDIS_PORT
})

export async function saveOtp(email, otp) {
  const key = `otp:${email}`
  const ttl = parseInt(process.env.OTP_TTL_SECONDS || '300')
  await redis.setex(key, ttl, otp)
}

export async function verifyOtp(email, otp) {
  const key = `otp:${email}`
  const stored = await redis.get(key)
  if (!stored) return false
  return stored === otp
}

export async function removeOtp(email) {
  const key = `otp:${email}`
  await redis.del(key)
}

export default redis
