import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Logger } from '@nestjs/common';
import { Job } from 'bullmq';
import { QUEUE_NOTIFICATION } from '../infrastructure/queue/queue.service';
import { workerOptionsFor } from '../infrastructure/queue/queue-definitions';
import { NotificationsService } from '../modules/notifications/notifications.service';

@Processor(QUEUE_NOTIFICATION, workerOptionsFor(QUEUE_NOTIFICATION))
export class NotificationWorker extends WorkerHost {
  private readonly logger = new Logger(NotificationWorker.name);

  constructor(
    private readonly notificationsService: NotificationsService,
  ) {
    super();
  }

  async process(job: Job<{ userId: string; type: string; title: string; body: string }>) {
    this.logger.log(`Processing notification job ${job.id}`);
    await this.notificationsService.send(job.data);
    this.logger.log(`Notification job ${job.id} completed`);
  }
}
