       -> importPacks ->
         console.log 'All Packs are added'
         queue.next()
-      -> createBot ->
+      -> createBot (err) ->
+        console.error err  if err
         queue.next()
       -> process.exit(1)
     ]
 
     daisy queue
     
-
 initProducts()
 
  

importProducts = (callback) ->
  data = require(__dirname + '/products');

  queue = []
  insertProducts = race (i, product, fin) ->
    {title} = product  
    {JPaymentProduct} = worker.models
    JPaymentProduct.one title: title, (err, paymentProduct) ->
      return console.error err if err
      if paymentProduct? 
        console.log "#{product.title} product is already added" 
        fin()
      else
        JPaymentProduct.create "koding", product, (err) ->
          console.log "#{product.title} product added successfully" unless err
          fin()
  , -> callback null

  insertProducts product for product in data

importPacks = (callback) ->
  data = require(__dirname + '/packs');

  queue = []
  fetchAllProducts (err, productPlanCodes) ->
    return console.error err if err
    insertPacks = race (i, pack, fin) ->
      {title} = pack  
      {JPaymentPack, JPaymentProduct} = worker.models
      JPaymentPack.one title: title, (err, paymentPack) ->
        return console.error err if err
        if paymentPack? 
          console.log "#{pack.title} pack is already added" 
          fin()
        else
          JPaymentPack.create "koding", pack, (err, pack) ->
            return console.error err if err
            console.log "#{pack.title} pack added successfully" 

            quantities = {}
            if pack.title is "VM"
              quantities[productPlanCodes["CPU"]] = 1
              quantities[productPlanCodes["RAM"]] = 1
              quantities[productPlanCodes["VM"]] = 1
            else
              quantities[productPlanCodes[pack.title]] = 1
            pack.updateProducts quantities, (err) ->
              return console.error err if err
              console.log "#{pack.title} pack products are added"
              fin()
            
    , -> callback null



  fetchAllProducts (err, productPlanCodes) ->
    return console.error err if err

    queue = []
    insertPlans = race (i, plan, fin) ->
      kreate = ->
        JPaymentPlan.create "koding", plan, (err, newPlan) ->
          if err 
            console.error err
            return fin()

          console.log "#{plan.title} plan added successfully" 
          quantities = {}
          if plan.title is "Team Plan" 
            quantities[productPlanCodes["CPU"]] = 4
            quantities[productPlanCodes["RAM"]] = 2
            quantities[productPlanCodes["Disk"]] = 50
            quantities[productPlanCodes["Always On"]] = 1
            quantities[productPlanCodes["Max VM"]] = 10
            quantities[productPlanCodes["User"]] = 1
            quantities[productPlanCodes["Group"]] = 1
          else
            {count} = plan
            quantities[productPlanCodes["CPU"]] = count * 4
            quantities[productPlanCodes["RAM"]] = count * 2
            quantities[productPlanCodes["Disk"]] = count * 50
            quantities[productPlanCodes["Always On"]] = count
            quantities[productPlanCodes["Max VM"]] = count * 10
          
          console.log 'quantities', quantities
          newPlan.updateProducts quantities, (err) ->
            if err then console.error err
            else console.log "#{plan.title} plan products are added"
            fin()

      {title} = plan  
      JPaymentPlan.one title: title, (err, paymentPlan) ->
        if err 
          console.error err
          return fin()

        if paymentPlan? 
          paymentPlan.remove (err) ->
            if err
              console.error "#{plan.title} cannot be added" 
              fin()
            else
              kreate()
        else
          kreate()
    , -> callback null

    insertPlans plan for plan in data
