/* eslint-disable no-console */

import cron from 'node-cron';

const CRON_OPTIONS_IST = {
  timezone: 'Asia/Kolkata',
};

/**
 * A wrapper around cron.schedule that enforces IST timezone.
 * * @param {string} schedule - The cron pattern (e.g., '30 9 * * *').
 * @param {Function} taskFn - The async function to execute.
 * @param {string} logMessage - The log message to display on execution.
 */
export const scheduleIstTask = (
  schedule: string,
  taskFn,
  logMessage: string,
) => {
  cron.schedule(
    schedule,
    async () => {
      try {
        await taskFn();
        console.log(logMessage);
      } catch (error) {
        console.error(`Error running cron task ${logMessage}:`, error);
      }
    },
    CRON_OPTIONS_IST, 
  );
};
