/// <reference path="../pb_data/types.d.ts" />
// Creates the on_demand_attempts collection. Stores one record per completed
// on-demand topic test. Separate from `test_results` so the weekly-test flow
// is unaffected. Used by testService.finishOnDemandTest, the Contributions
// activity feed, and the anti-farming weekly cap aggregation.
migrate((app) => {
  const collection = new Collection({
    "createRule": "",
    "deleteRule": "",
    "listRule":   "",
    "updateRule": "",
    "viewRule":   "",
    "name": "on_demand_attempts",
    "type": "base",
    "fields": [
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_user_id",
        "max": 0,
        "min": 0,
        "name": "user_id",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": true,
        "system": false,
        "type": "text"
      },
      {
        "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"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_topic_label",
        "max": 0,
        "min": 0,
        "name": "topic_label",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": false,
        "system": false,
        "type": "text"
      },
      {
        "hidden": false,
        "id": "json_question_ids",
        "maxSize": 0,
        "name": "question_ids",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "number_score",
        "max": null,
        "min": null,
        "name": "score",
        "onlyInt": false,
        "presentable": false,
        "required": false,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "number_total",
        "max": null,
        "min": null,
        "name": "total",
        "onlyInt": false,
        "presentable": false,
        "required": false,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "number_percentage",
        "max": null,
        "min": null,
        "name": "percentage",
        "onlyInt": false,
        "presentable": false,
        "required": false,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "number_time_used",
        "max": null,
        "min": null,
        "name": "time_used",
        "onlyInt": false,
        "presentable": false,
        "required": false,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "number_unseen_correct",
        "max": null,
        "min": null,
        "name": "unseen_correct",
        "onlyInt": false,
        "presentable": false,
        "required": false,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "number_points_earned",
        "max": null,
        "min": null,
        "name": "points_earned",
        "onlyInt": false,
        "presentable": false,
        "required": false,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "bool_capped",
        "name": "capped",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "bool"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_iso_week",
        "max": 0,
        "min": 0,
        "name": "iso_week",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": false,
        "system": false,
        "type": "text"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_completed_at",
        "max": 0,
        "min": 0,
        "name": "completed_at",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": false,
        "system": false,
        "type": "text"
      },
      {
        "hidden": false,
        "id": "json_breakdown",
        "maxSize": 0,
        "name": "breakdown",
        "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("on_demand_attempts");
  return app.delete(collection);
});
