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

  const themeSessions = new Collection({
    "id": "pbc_theme_sessions_0",
    "name": "theme_sessions",
    "type": "base",
    "system": false,
    "fields": [
      {
        "autogeneratePattern": "[a-z0-9]{15}",
        "hidden": false,
        "id": "text_id_ts",
        "max": 15,
        "min": 15,
        "name": "id",
        "pattern": "^[a-z0-9]+$",
        "presentable": false,
        "primaryKey": true,
        "required": true,
        "system": true,
        "type": "text"
      },
      {
        "system": false,
        "id": "rel_curriculum_version",
        "name": "curriculum_version",
        "type": "relation",
        "required": true,
        "presentable": false,
        "collectionId": curriculumVersionsCollectionId,
        "cascadeDelete": true,
        "minSelect": null,
        "maxSelect": 1,
        "displayFields": null
      },
      {
        "hidden": false,
        "id": "num_week_number_ts",
        "max": 26,
        "min": 1,
        "name": "week_number",
        "onlyInt": true,
        "presentable": false,
        "required": true,
        "system": false,
        "type": "number"
      },
      {
        "system": false,
        "id": "text_theme_ts",
        "name": "theme",
        "type": "text",
        "required": true,
        "presentable": false,
        "max": 0,
        "min": 0,
        "pattern": ""
      },
      {
        "system": false,
        "id": "json_content_ts",
        "name": "content",
        "type": "json",
        "required": true,
        "presentable": false
      },
      {
        "hidden": false,
        "id": "autodate_created_ts",
        "name": "created",
        "onCreate": true,
        "onUpdate": false,
        "presentable": false,
        "system": true,
        "type": "autodate"
      },
      {
        "hidden": false,
        "id": "autodate_updated_ts",
        "name": "updated",
        "onCreate": true,
        "onUpdate": true,
        "presentable": false,
        "system": true,
        "type": "autodate"
      }
    ],
    "indexes": [
      "CREATE UNIQUE INDEX `idx_theme_sessions_version_week` ON `theme_sessions` (`curriculum_version`, `week_number`)"
    ],
    "listRule": "",
    "viewRule": "",
    "createRule": "",
    "updateRule": "",
    "deleteRule": ""
  });

  app.save(themeSessions);

  const themeSessionCompletions = new Collection({
    "id": "pbc_theme_session_completions_0",
    "name": "theme_session_completions",
    "type": "base",
    "system": false,
    "fields": [
      {
        "autogeneratePattern": "[a-z0-9]{15}",
        "hidden": false,
        "id": "text_id_tsc",
        "max": 15,
        "min": 15,
        "name": "id",
        "pattern": "^[a-z0-9]+$",
        "presentable": false,
        "primaryKey": true,
        "required": true,
        "system": true,
        "type": "text"
      },
      {
        "system": false,
        "id": "rel_tsc_team_member",
        "name": "team_member_id",
        "type": "relation",
        "required": true,
        "presentable": false,
        "collectionId": teamMembersCollectionId,
        "cascadeDelete": true,
        "minSelect": null,
        "maxSelect": 1,
        "displayFields": null
      },
      {
        "system": false,
        "id": "rel_tsc_theme_session",
        "name": "theme_session_id",
        "type": "relation",
        "required": true,
        "presentable": false,
        "collectionId": themeSessions.id,
        "cascadeDelete": true,
        "minSelect": null,
        "maxSelect": 1,
        "displayFields": null
      },
      {
        "hidden": false,
        "id": "num_tsc_session_week",
        "max": null,
        "min": 1,
        "name": "session_week",
        "onlyInt": true,
        "presentable": false,
        "required": true,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "autodate_created_tsc",
        "name": "created",
        "onCreate": true,
        "onUpdate": false,
        "presentable": false,
        "system": true,
        "type": "autodate"
      },
      {
        "hidden": false,
        "id": "autodate_updated_tsc",
        "name": "updated",
        "onCreate": true,
        "onUpdate": true,
        "presentable": false,
        "system": true,
        "type": "autodate"
      }
    ],
    "indexes": [
      "CREATE UNIQUE INDEX `idx_theme_session_completions_user_week` ON `theme_session_completions` (`team_member_id`, `session_week`)"
    ],
    "listRule": "",
    "viewRule": "",
    "createRule": "",
    "updateRule": "",
    "deleteRule": ""
  });

  app.save(themeSessionCompletions);

}, (app) => {
  const themeSessionCompletions = app.findCollectionByNameOrId("theme_session_completions");
  if (themeSessionCompletions) {
    app.delete(themeSessionCompletions);
  }
  const themeSessions = app.findCollectionByNameOrId("theme_sessions");
  if (themeSessions) {
    app.delete(themeSessions);
  }
})
