import cron from 'node-cron'
import type { Payload } from 'payload'
import { runConsentRetentionJob } from './consentRetentionJob'

/**
 * Initialisiert alle Scheduled Jobs
 */
export const initScheduledJobs = (payload: Payload): void => {
  // Consent Retention: Täglich um 03:00 Uhr
  cron.schedule(
    '0 3 * * *',
    async () => {
      console.log('[Scheduler] Starte Consent Retention Job...')
      try {
        await runConsentRetentionJob(payload)
      } catch (error) {
        console.error('[Scheduler] Consent Retention Job fehlgeschlagen:', error)
      }
    },
    {
      timezone: 'Europe/Berlin',
    },
  )

  console.log('[Scheduler] Scheduled Jobs initialisiert.')
}
