// Weekly automatic invite rotation (Sundays at midnight). A missing invite
// channel config just skips quietly (warn-logged) — most guilds won't set
// this up on day one, and that shouldn't spam errors every week until they do.
const cron = require('node-cron')

const inviteRotator = require('./inviteRotator')
const createLogger = require('../utils/logger')

const log = createLogger('invites')

let task = null

function start(client, guildId) {
  task = cron.schedule('0 0 * * 0', async () => {
    try {
      await inviteRotator.rotate(client, guildId, {})
    } catch (err) {
      log.warn('scheduled invite rotation skipped', { message: err.message })
    }
  })
  log.info('invite rotation scheduler started')
}

function stop() {
  if (task) {
    task.stop()
    task = null
  }
}

module.exports = { start, stop }
