/// <reference path="../pb_data/types.d.ts" />
// Creates the question_bank collection used by on-demand topic tests.
// One record per topic; `questions` is a JSON array of question objects
// (id, question, options[], correctIndex, explanation, difficulty, topicLabel).
// Replaces the deprecated `quiz_banks` collection that was dropped in
// 1780800001_deleted_legacy_collections.js.
migrate((app) => {
  const collection = new Collection({
    "createRule": "",
    "deleteRule": "",
    "listRule":   "",
    "updateRule": "",
    "viewRule":   "",
    "name": "question_bank",
    "type": "base",
    "fields": [
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_topic_id",
        "max": 0,
        "min": 0,
        "name": "topic_id",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": true,
        "system": false,
        "type": "text"
      },
      {
        "hidden": false,
        "id": "json_questions",
        "maxSize": 0,
        "name": "questions",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "autodate_created",
        "name": "created",
        "onCreate": true,
        "onUpdate": false,
        "presentable": false,
        "system": false,
        "type": "autodate"
      },
      {
        "hidden": false,
        "id": "autodate_updated",
        "name": "updated",
        "onCreate": true,
        "onUpdate": true,
        "presentable": false,
        "system": false,
        "type": "autodate"
      }
    ]
  });

  return app.save(collection);
}, (app) => {
  const collection = app.findCollectionByNameOrId("question_bank");
  return app.delete(collection);
});
