const User = require('./models/user/User')
const Cart_Item = require('./models/user/Cart_item')

const minute = 1000 * 60
const hour = minute * 60
const day = hour * 24

module.exports = () => {
   // check users for activation
   setInterval(() => {
      User.find({confirmed: false})
         .then(users => {
            if (users.length) {
               ///////////////////////
               users.forEach((item, index) => {
                  // item time and timeout
                  const itemTime = Date.parse(item.created_at)
                  const timeout = itemTime + day
                  ////////////////////////////
                  if (Date.now() >= timeout) {
                     item.remove()
                  }
                  if (index === users.length - 1) {
                     console.log('Computing Done.')
                  }
               })
            } else {
               console.log('Computing Done.')
            }
         })
         .catch(err => {
            console.log(err)
         })
   }, hour)

   // check cart items
   setInterval(() => {
      Cart_Item.find({})
         .then(cart_items => {
            if (cart_items.length) {
               ///////////////////////
               cart_items.forEach((item, index) => {
                  // item time and timeout
                  const itemTime = Date.parse(item.created_at)
                  const timeout = itemTime + (day * 7)
                  ////////////////////////////
                  if (Date.now() >= timeout) {
                     item.remove()
                  }
                  if (index === cart_items.length - 1) {
                     console.log('Computing Done.')
                  }
               })
            } else {
               console.log('Computing Done.')
            }
         })
         .catch(err => {
            console.log(err)
         })
   }, day)
}
