/// <reference path="../pb_data/types.d.ts" />
migrate((app) => {
  // ── 1. Create curriculum_versions collection ──────────────────────────────
  const curriculumVersions = new Collection({
    "createRule": "",
    "deleteRule": "",
    "fields": [
      {
        "autogeneratePattern": "[a-z0-9]{15}",
        "hidden": false,
        "id": "text3208210256",
        "max": 15,
        "min": 15,
        "name": "id",
        "pattern": "^[a-z0-9]+$",
        "presentable": false,
        "primaryKey": true,
        "required": true,
        "system": true,
        "type": "text"
      },
      {
        "hidden": false,
        "id": "number_version_number",
        "max": null,
        "min": 1,
        "name": "version_number",
        "onlyInt": true,
        "presentable": true,
        "required": true,
        "system": false,
        "type": "number"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_status",
        "max": 20,
        "min": 0,
        "name": "status",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": true,
        "system": false,
        "type": "text"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_generation_reason",
        "max": 0,
        "min": 0,
        "name": "generation_reason",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": false,
        "system": false,
        "type": "text"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_confirmed_by",
        "max": 0,
        "min": 0,
        "name": "confirmed_by",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": false,
        "system": false,
        "type": "text"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text_confirmed_at",
        "max": 0,
        "min": 0,
        "name": "confirmed_at",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": false,
        "system": false,
        "type": "text"
      },
      {
        "hidden": false,
        "id": "json_schedule",
        "maxSize": 0,
        "name": "schedule",
        "presentable": false,
        "required": true,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json_coverage_stats",
        "maxSize": 0,
        "name": "coverage_stats",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "autodate2990389176",
        "name": "created",
        "onCreate": true,
        "onUpdate": false,
        "presentable": false,
        "system": false,
        "type": "autodate"
      },
      {
        "hidden": false,
        "id": "autodate3332085495",
        "name": "updated",
        "onCreate": true,
        "onUpdate": true,
        "presentable": false,
        "system": false,
        "type": "autodate"
      }
    ],
    "id": "pbc_curriculum_v2",
    "indexes": [],
    "listRule": "",
    "name": "curriculum_versions",
    "system": false,
    "type": "base",
    "updateRule": "",
    "viewRule": ""
  });

  app.save(curriculumVersions);

  // ── 2. Add theme, complexity_weight, difficulty to topics ─────────────────
  const topics = app.findCollectionByNameOrId("topics");

  topics.fields.add(new Field({
    "autogeneratePattern": "",
    "hidden": false,
    "id": "text_theme",
    "max": 0,
    "min": 0,
    "name": "theme",
    "pattern": "",
    "presentable": false,
    "primaryKey": false,
    "required": false,
    "system": false,
    "type": "text"
  }));

  topics.fields.add(new Field({
    "hidden": false,
    "id": "number_complexity_weight",
    "max": 5,
    "min": 1,
    "name": "complexity_weight",
    "onlyInt": true,
    "presentable": false,
    "required": false,
    "system": false,
    "type": "number"
  }));

  topics.fields.add(new Field({
    "autogeneratePattern": "",
    "hidden": false,
    "id": "text_difficulty",
    "max": 20,
    "min": 0,
    "name": "difficulty",
    "pattern": "",
    "presentable": false,
    "primaryKey": false,
    "required": false,
    "system": false,
    "type": "text"
  }));

  app.save(topics);

}, (app) => {
  // ── Rollback: remove curriculum_versions collection ────────────────────────
  const curriculumVersions = app.findCollectionByNameOrId("curriculum_versions");
  app.delete(curriculumVersions);

  // ── Rollback: remove new fields from topics ───────────────────────────────
  const topics = app.findCollectionByNameOrId("topics");
  topics.fields.removeById("text_theme");
  topics.fields.removeById("number_complexity_weight");
  topics.fields.removeById("text_difficulty");
  app.save(topics);
});
