// src/producers/notification.producer.ts

import { NotificationJobType, NotificationQueue } from '@/jobs/notifications/notification.job.js';
import config from '@/shared/config/config.js';
import { EmailType } from '@/shared/constants/enum.constant.js';

export const sendEmailNotification = async (payload: {
  to: string;
  subject: string;
  body: string;
  type: (typeof EmailType)[number];
}) => {
  await NotificationQueue.add(NotificationJobType.SEND_EMAIL, payload, {
    attempts: 3,
    backoff: {
      type: 'exponential',
      delay: 3000,
    },
    ...(config.env === 'production' && {
      removeOnComplete: {
        age: 3600, // remove after 1 hour
      },
    }),
    ...(config.env === 'production' && {
      removeOnFail: {
        age: 86400, // remove after 1 day
      },
    }),
    ...(payload.type === EmailType?.[0] && {
      priority: 10000,
    }),
    ...(payload.type === EmailType?.[1] && {
      timeout: 10_000,
    }),
  });
};

export const sendPlanStatusNotification = async (payload: {
  to: string;
  subject: string;
  body: string;
  type: (typeof EmailType)[number];
}) => {
  await NotificationQueue.add(NotificationJobType.SEND_EMAIL, payload, {
    attempts: 3,
    backoff: {
      type: 'exponential',
      delay: 3000,
    },    ...(config.env === 'production' && {
      removeOnComplete: {
        age: 3600, // remove after 1 hour
      },
    }),
    ...(config.env === 'production' && {
      removeOnFail: {
        age: 86400, // remove after 1 day
      },
    }),
    ...(payload.type === EmailType?.[0] && {
      priority: 10000,
    }),
    ...(payload.type === EmailType?.[1] && {
      timeout: 10_000,
    }),
  });
};

//TODO: optimize for SMS notifications
// export const sendSmsNotification = async (payload: { phone: string; message: string }) => {
//   await NotificationQueue.add(NotificationJobType.SEND_SMS, payload);
// };
