import { creditNoteSyncService } from '../services/creditNoteSync.service';
import logger from '../utils/logger';

/**
 * Main sync function to process all outgoing files
 * Delegates to creditNoteSyncService
 */
export async function syncCreditNotes(): Promise<void> {
  await creditNoteSyncService.syncCreditNotes();
}

/**
 * Starts the background sync job
 */
export function startCreditNoteSyncJob(): void {
  const intervalMinutes = Number(process.env.CREDIT_NOTE_SYNC_INTERVAL_MINUTES) || 5;
  logger.info(`[CreditNoteSyncJob] Background job initialized (Interval: ${intervalMinutes}m)`);
  
  // Run once immediately on startup
  syncCreditNotes().catch(err => logger.error('[CreditNoteSyncJob] Initial sync failed:', err));
  
  setInterval(() => {
    syncCreditNotes().catch(err => logger.error('[CreditNoteSyncJob] Periodic sync failed:', err));
  }, intervalMinutes * 60 * 1000);
}
