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

export const offboardingLwdWorker = new Worker(
    'offboardingLwdQueue',
    async (job) => {
        console.log(`[Offboarding LWD Worker] Processing job ${job.id} (${job.name})`);

        if (job.name === 'checkLwdFnfReminders') {
            await OffboardingLwdReminderService.processAllPendingReminders();
            return;
        }

        if (job.name === 'sendLwdFnfReminder') {
            const payload = job.data as { requestType: 'resignation' | 'termination'; requestId: string };
            await OffboardingLwdReminderService.processOne(payload);
            return;
        }

        console.warn(`[Offboarding LWD Worker] Unknown job name: ${job.name}`);
    },
    {
        connection: redisConfig,
        concurrency: 2
    }
);

offboardingLwdWorker.on('completed', (job) => {
    console.log(`[Offboarding LWD Worker] Job ${job.id} completed`);
});

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