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

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

/**
 * Scheduled prospect reminders for pending questionnaires (not internal SLA).
 * Default: daily 09:00 — or every 15 min when DEBUG_QUESTIONNAIRE_REMINDER_FAST_MODE=true.
 */
export const scheduleQuestionnaireReminders = async () => {
    const isFastMode = process.env.DEBUG_QUESTIONNAIRE_REMINDER_FAST_MODE === 'true';
    const pattern = isFastMode ? '*/15 * * * *' : '0 9 * * *';

    const jobs = await questionnaireReminderQueue.getRepeatableJobs();
    for (const job of jobs) {
        await questionnaireReminderQueue.removeRepeatableByKey(job.key);
    }

    await questionnaireReminderQueue.add('checkQuestionnaireReminders', {}, {
        repeat: { pattern },
        jobId: 'questionnaire-pending-reminder-sweep'
    });

    console.log(
        `[Questionnaire Reminder Queue] Scheduled: ${isFastMode ? 'every 15 min (FAST MODE)' : 'daily at 09:00'}`
    );
};
