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

export const questionnaireReminderWorker = new Worker(
    'questionnaireReminderQueue',
    async (job) => {
        console.log(`[Questionnaire Reminder Worker] Processing job ${job.id} (${job.name})`);

        if (job.name === 'checkQuestionnaireReminders') {
            await QuestionnaireReminderService.processScheduledReminders();
            return;
        }

        console.warn(`[Questionnaire Reminder Worker] Unknown job name: ${job.name}`);
    },
    {
        connection: redisConfig,
        concurrency: 1
    }
);

questionnaireReminderWorker.on('completed', (job) => {
    console.log(`[Questionnaire Reminder Worker] Job ${job.id} completed`);
});

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