/// <reference path="../pb_data/types.d.ts" />
migrate((app) => {
  const collection = app.findCollectionByNameOrId("pbc_1124997656")

  if (collection) {
    // Check if created already exists
    let hasCreated = false;
    for (let f of collection.fields) {
      if (f.name === 'created') hasCreated = true;
    }
    
    if (!hasCreated) {
      collection.fields.addAt(4, new Field({
        "hidden": false,
        "id": "autodate_sources_created",
        "name": "created",
        "onCreate": true,
        "onUpdate": false,
        "presentable": false,
        "system": false,
        "type": "autodate"
      }))

      collection.fields.addAt(5, new Field({
        "hidden": false,
        "id": "autodate_sources_updated",
        "name": "updated",
        "onCreate": true,
        "onUpdate": true,
        "presentable": false,
        "system": false,
        "type": "autodate"
      }))

      return app.save(collection)
    }
  }
}, (app) => {
  const collection = app.findCollectionByNameOrId("pbc_1124997656")
  if (collection) {
    collection.fields.removeById("autodate_sources_created")
    collection.fields.removeById("autodate_sources_updated")
    return app.save(collection)
  }
})
