/// <reference path="../pb_data/types.d.ts" />
migrate((app) => {
  const topicsCollectionId = "pbc_2800040823";
  const teamMembersCollectionId = "pbc_3980519374";

  // Create micro_learnings collection
  const microLearnings = new Collection({
    "id": "pbc_micro_learnings_0",
    "name": "micro_learnings",
    "type": "base",
    "system": false,
    "fields": [
      {
        "autogeneratePattern": "[a-z0-9]{15}",
        "help": "",
        "hidden": false,
        "id": "text_id_ml",
        "max": 15,
        "min": 15,
        "name": "id",
        "pattern": "^[a-z0-9]+$",
        "presentable": false,
        "primaryKey": true,
        "required": true,
        "system": true,
        "type": "text"
      },
      {
        "system": false,
        "id": "rel_topic_id",
        "name": "topic_id",
        "type": "relation",
        "required": true,
        "presentable": false,
        "collectionId": topicsCollectionId,
        "cascadeDelete": true,
        "minSelect": null,
        "maxSelect": 1,
        "displayFields": null
      },
      {
        "system": false,
        "id": "sel_type",
        "name": "type",
        "type": "select",
        "required": true,
        "presentable": false,
        "maxSelect": 1,
        "values": [
          "concept_explainer",
          "scenario_quiz",
          "flashcard_set",
          "reflection_prompt"
        ]
      },
      {
        "system": false,
        "id": "json_content",
        "name": "content",
        "type": "json",
        "required": true,
        "presentable": false
      },
      {
        "system": false,
        "id": "sel_status",
        "name": "status",
        "type": "select",
        "required": true,
        "presentable": false,
        "maxSelect": 1,
        "values": [
          "draft",
          "published"
        ]
      },
      {
        "hidden": false,
        "id": "autodate_created",
        "name": "created",
        "onCreate": true,
        "onUpdate": false,
        "presentable": false,
        "system": true,
        "type": "autodate"
      },
      {
        "hidden": false,
        "id": "autodate_updated",
        "name": "updated",
        "onCreate": true,
        "onUpdate": true,
        "presentable": false,
        "system": true,
        "type": "autodate"
      }
    ],
    "indexes": [],
    "listRule": "status = 'published'",
    "viewRule": "status = 'published'",
    "createRule": null,
    "updateRule": null,
    "deleteRule": null
  });

  app.save(microLearnings);

  // Create micro_learning_completions collection
  const completions = new Collection({
    "id": "pbc_ml_completions_0",
    "name": "micro_learning_completions",
    "type": "base",
    "system": false,
    "fields": [
      {
        "autogeneratePattern": "[a-z0-9]{15}",
        "help": "",
        "hidden": false,
        "id": "text_id_mlc",
        "max": 15,
        "min": 15,
        "name": "id",
        "pattern": "^[a-z0-9]+$",
        "presentable": false,
        "primaryKey": true,
        "required": true,
        "system": true,
        "type": "text"
      },
      {
        "system": false,
        "id": "rel_team_member_id",
        "name": "team_member_id",
        "type": "relation",
        "required": true,
        "presentable": false,
        "collectionId": teamMembersCollectionId,
        "cascadeDelete": true,
        "minSelect": null,
        "maxSelect": 1,
        "displayFields": null
      },
      {
        "system": false,
        "id": "rel_micro_learning_id",
        "name": "micro_learning_id",
        "type": "relation",
        "required": true,
        "presentable": false,
        "collectionId": microLearnings.id,
        "cascadeDelete": true,
        "minSelect": null,
        "maxSelect": 1,
        "displayFields": null
      },
      {
        "system": false,
        "id": "rel_comp_topic_id",
        "name": "topic_id",
        "type": "relation",
        "required": true,
        "presentable": false,
        "collectionId": topicsCollectionId,
        "cascadeDelete": true,
        "minSelect": null,
        "maxSelect": 1,
        "displayFields": null
      },
      {
        "system": false,
        "id": "txt_comp_type",
        "name": "type",
        "type": "text",
        "required": true,
        "presentable": false,
        "min": null,
        "max": null,
        "pattern": ""
      },
      {
        "system": false,
        "id": "num_session_week",
        "name": "session_week",
        "type": "number",
        "required": true,
        "presentable": false,
        "noDecimal": true
      },
      {
        "hidden": false,
        "id": "autodate_created2",
        "name": "created",
        "onCreate": true,
        "onUpdate": false,
        "presentable": false,
        "system": true,
        "type": "autodate"
      },
      {
        "hidden": false,
        "id": "autodate_updated2",
        "name": "updated",
        "onCreate": true,
        "onUpdate": true,
        "presentable": false,
        "system": true,
        "type": "autodate"
      }
    ],
    "indexes": [],
    "listRule": "",
    "viewRule": "",
    "createRule": "",
    "updateRule": "",
    "deleteRule": ""
  });

  app.save(completions);

}, (app) => {
  const completions = app.findCollectionByNameOrId("micro_learning_completions");
  if (completions) {
    app.delete(completions);
  }
  const microLearnings = app.findCollectionByNameOrId("micro_learnings");
  if (microLearnings) {
    app.delete(microLearnings);
  }
})
