import Redis from 'ioredis';
import { log } from './logger.js';

const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';

const redis = new Redis(REDIS_URL, {
  maxRetriesPerRequest: null,
  retryStrategy: (times) => {
    if (times > 3) return null; // stop retrying after 3 attempts
    return Math.min(times * 200, 1000);
  },
});

redis.on('error', (err) => {
  // Suppress ECONNREFUSED noise after initial failure — only warn
  if (err.code !== 'ECONNREFUSED') {
    log.redis.warn(`DragonflyDB error: ${err.message}`);
  }
});

redis.on('connect', () => {
  log.redis.info('DragonflyDB connected');
});

export default redis;
