// In-memory OTP store with TTL (replaces Redis; single-process only).
const store = new Map<string, { value: string; timer: NodeJS.Timeout }>();

export const otpStore = {
  setEx(key: string, ttlSeconds: number, value: string) {
    otpStore.del(key);
    const timer = setTimeout(() => store.delete(key), ttlSeconds * 1000);
    timer.unref();
    store.set(key, { value, timer });
  },
  get(key: string): string | null {
    return store.get(key)?.value ?? null;
  },
  del(key: string) {
    const entry = store.get(key);
    if (entry) clearTimeout(entry.timer);
    store.delete(key);
  },
};
