import Redis from 'ioredis';
import {
    getRedisHost,
    getRedisPassword,
    getRedisPort,
    isRedisTlsEnabled,
} from './redis.config';

const host = getRedisHost();
const useTls = isRedisTlsEnabled();

const redisClient = new Redis({
    host,
    port: getRedisPort(),
    password: getRedisPassword(),
    tls: useTls ? { servername: host } : undefined,
    family: 4,
});

redisClient.on('connect', () => {
    console.log('✅ Connected to Redis');
});

redisClient.on('error', (err) => {
    console.error('❌ Redis connection error:', err);
});

redisClient.on('end', () => {
    console.log('❌ Redis connection closed');
});

export { redisClient };
