import { Redis } from "ioredis";
import { env } from "./env.js";

type RedisClient = InstanceType<typeof Redis>;

let client: RedisClient | null = null;

export function getRedis(): RedisClient {
  if (!client) {
    client = new Redis(env.REDIS_URL, {
      maxRetriesPerRequest: 3,
      enableReadyCheck: true,
      lazyConnect: false,
    });
  }
  return client;
}

export async function closeRedis(): Promise<void> {
  if (client) {
    await client.quit();
    client = null;
  }
}
