
import Redis from 'ioredis';
import { Queue, Worker, QueueOptions, WorkerOptions, Processor } from 'bullmq';

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

const redisConfig = {
    host: REDIS_HOST,
    port: REDIS_PORT,
    password: REDIS_PASSWORD,
    maxRetriesPerRequest: null, // Essential for BullMQ
};

// Singleton Redis connection for general use if needed
export const redisConnection = new Redis(redisConfig);

// Helper to create Queue
export const createQueue = (name: string, options?: QueueOptions) => {
    return new Queue(name, {
        connection: redisConfig,
        ...options
    });
};

// Helper to create Worker
export const createWorker = (name: string, processor: Processor, options?: Omit<WorkerOptions, 'connection'>) => {
    return new Worker(name, processor, {
        connection: redisConfig,
        ...options
    });
};

export default redisConfig;
