import { Redis } from 'ioredis';

const redisHost = process.env.REDIS_HOST || 'localhost';
const redisPort = parseInt(process.env.REDIS_PORT || '6379', 10);

export const redis = new Redis({
  host: redisHost,
  port: redisPort,
  retryStrategy: (times: number) => Math.min(times * 50, 2000),
  lazyConnect: true,
});

export async function getRedisConnection(): Promise<Redis> {
  if (redis.status === 'wait' || redis.status === 'connecting') {
    await redis.connect();
  }
  return redis;
}
