/// <reference path="../pb_data/types.d.ts" />
migrate(
  (app) => {
    // The real food menu uses text prices ("449/549" for Veg/Chicken variants)
    // and needs a veg/non-veg badge field. The previous menu_items collection
    // only held placeholder content, so we recreate it with the final schema.
    try {
      const oldCollection = app.findCollectionByNameOrId('menu_items')
      app.delete(oldCollection)
    } catch (e) {
      // collection may not exist on a fresh database — nothing to migrate
    }

    const collection = new Collection({
      name: 'menu_items',
      type: 'base',
      listRule: '',
      viewRule: '',
      createRule: null,
      updateRule: null,
      deleteRule: null,
      fields: [
        { name: 'uid', type: 'text', required: true },
        { name: 'category', type: 'text', required: true },
        { name: 'name', type: 'text', required: true },
        { name: 'description', type: 'text' },
        { name: 'price', type: 'text' },
        { name: 'pairing', type: 'text' },
        { name: 'tag', type: 'text' },
        { name: 'image', type: 'text' },
        { name: 'group', type: 'text' },
        { name: 'sort', type: 'number' },
      ],
      indexes: ['CREATE UNIQUE INDEX idx_menu_items_uid ON menu_items (uid)'],
    })
    return app.save(collection)
  },
  (app) => {
    // Revert to the original placeholder schema.
    try {
      const newCollection = app.findCollectionByNameOrId('menu_items')
      app.delete(newCollection)
    } catch (e) {
      // nothing to revert
    }

    const collection = new Collection({
      name: 'menu_items',
      type: 'base',
      listRule: '',
      viewRule: '',
      createRule: null,
      updateRule: null,
      deleteRule: null,
      fields: [
        { name: 'uid', type: 'text', required: true },
        { name: 'category', type: 'text', required: true },
        { name: 'name', type: 'text', required: true },
        { name: 'description', type: 'text' },
        { name: 'price', type: 'number' },
        { name: 'pairing', type: 'text' },
        { name: 'tag', type: 'text' },
        { name: 'image', type: 'text' },
        { name: 'sort', type: 'number' },
      ],
      indexes: ['CREATE UNIQUE INDEX idx_menu_items_uid ON menu_items (uid)'],
    })
    return app.save(collection)
  }
)
