const { runRetention } = require("./enforceCoachingRetention");
const { runReminderSchedule } = require("./scheduleCoachingReminders");

const reminderIntervalMs = 60 * 1000;
const retentionIntervalMs = 24 * 60 * 60 * 1000;

function runScheduledJob(name, job) {
  job().catch((error) => {
    console.error(`${name} failed`, error);
  });
}

function startCoachingScheduler() {
  if (process.env.DEMO_MODE === "true") {
    console.log("Coaching scheduler disabled in read-only demo mode");
    return;
  }

  runScheduledJob("Coaching reminder scheduling", runReminderSchedule);
  runScheduledJob("Coaching retention", runRetention);

  const reminderTimer = setInterval(() => {
    runScheduledJob("Coaching reminder scheduling", runReminderSchedule);
  }, reminderIntervalMs);
  reminderTimer.unref();

  const retentionTimer = setInterval(() => {
    runScheduledJob("Coaching retention", runRetention);
  }, retentionIntervalMs);
  retentionTimer.unref();
}

module.exports = { startCoachingScheduler };
