import {
  checkCompletedTasksAndNotify,
  notify2hoursUpcomingActivities,
} from '../services/tasks.notifications.job';
import { markOverdueTasks } from '../services/tasks.job';
import {
  runDailyTaskSummary,
  sendDailyTaskSummaryNotification,
  sendWeeklyTaskSummaryNotification,
} from '../services/taskdDailySummary.job';
import { scheduleIstTask } from './cron.service';
import { runEodWorkSummary } from '../services/eodWorkSummary.job';

const runMidnightTasks = async () => {
  await notify2hoursUpcomingActivities();
  await checkCompletedTasksAndNotify(3);
  await checkCompletedTasksAndNotify(7);
  await runDailyTaskSummary();
};

export const initTasksCronJob = () => {
  // 1. Every day at midnight IST (00:00)
  scheduleIstTask(
    '0 0 * * *',
    runMidnightTasks,
    'Midnight notification and daily summary tasks completed',
  );

  // 2. Schedule task to run at the start of every hour IST (minute 0)
  scheduleIstTask('0 * * * *', markOverdueTasks, 'Overdue tasks updated');

  // 3. Daily Task Summary Notification at 9:30 AM IST
  scheduleIstTask(
    '30 9 * * *',
    sendDailyTaskSummaryNotification,
    'Notifications for daily tasks updated',
  );

  // 4. Weekly Task Summary Notification at 9:30 AM IST every Monday
  scheduleIstTask(
    '30 9 * * 1',
    sendWeeklyTaskSummaryNotification,
    'Notifications for weekly summary updated',
  );

  // 5. Daily EOD Work Summary at 8 PM IST
  scheduleIstTask(
    '0 20 * * *',
    runEodWorkSummary,
    'Daily EOD Work Summary',
  );
};
