import { Queue } from 'bullmq';
import { redisConfig } from './config.js';

export const slaQueue = new Queue('slaQueue', {
    connection: redisConfig
});

/**
 * Schedule the recurring SLA check (Every Hour)
 */
export const scheduleSLACheck = async () => {
    const isFastMode = process.env.DEBUG_SLA_FAST_MODE === 'true';
    const pattern = isFastMode ? '* * * * *' : '0 * * * *';
    
    // Clean up old repeatable jobs to ensure fresh schedule
    const jobs = await slaQueue.getRepeatableJobs();
    for (const job of jobs) {
        await slaQueue.removeRepeatableByKey(job.key);
    }
    
    await slaQueue.add('checkSLABreaches', {}, {
        repeat: {
            pattern: pattern, 
        },
        jobId: 'sla-check-job' 
    });
    console.log(`[SLA Queue] Repeatable job scheduled: ${isFastMode ? 'Every minute (FAST MODE)' : 'Hourly'}`);
};
