const cron = require('node-cron');
const { triggerImmediateSync } = require('./immediate');
const { processExpiredGracePeriods } = require('./graceExpiration');

let retryTimeout = null;

function initCron() {
  // Hourly whitelist reconciliation
  cron.schedule('0 * * * *', async () => {
    console.log("⏰ Starting hourly sync jobs...");
    
    // 1. Process expired grace periods
    await processExpiredGracePeriods();
    
    // 2. Whitelist reconciliation
    console.log("Starting whitelist reconciliation...");
    const { failCount } = await triggerImmediateSync();
    
    // 3. Schedule retry if there were failures
    if (failCount > 0) {
      console.log(`⏳ ${failCount} servers failed. Scheduling retry in 10 minutes...`);
      
      // Clear any existing retry timeout
      if (retryTimeout) {
        clearTimeout(retryTimeout);
      }
      
      // Retry failed servers after 10 minutes
      retryTimeout = setTimeout(async () => {
        console.log("🔄 Running retry sync for failed servers...");
        const { failCount: retryFailCount } = await triggerImmediateSync(true);
        
        if (retryFailCount > 0) {
          console.log(`⚠️ ${retryFailCount} servers still failing after retry.`);
        } else {
          console.log("✅ All previously failed servers now synced successfully.");
        }
      }, 10 * 60 * 1000); // 10 minutes
    }
    
    console.log("✅ Hourly sync jobs complete");
  });
}

module.exports = { initCron };
