import { Worker } from 'bullmq';
import { redisConfig } from './config.js';
import { SLAService } from '../../services/SLAService.js';

export const slaWorker = new Worker('slaQueue', async (job) => {
    console.log(`[SLA Worker] Processing job ${job.id} of type ${job.name}`);
    
    if (job.name === 'checkSLABreaches') {
        await SLAService.checkBreaches();
    }
}, {
    connection: redisConfig,
    concurrency: 1 // Single concurrency for SLA checks to avoid race conditions
});

slaWorker.on('completed', (job) => {
    console.log(`[SLA Worker] Job ${job.id} completed successfully`);
});

slaWorker.on('failed', (job, err) => {
    console.error(`[SLA Worker] Job ${job?.id} failed with error: ${err.message}`);
});
