/// <reference path="../pb_data/types.d.ts" />
migrate((app) => {
  const collection = app.findCollectionByNameOrId("pbc_1358894712")

  // update collection data
  unmarshal({
    "viewQuery": "SELECT\n      p.id as id,\n      p.id as player_id,\n      (p.first_name || ' ' || p.last_name) as player_name,\n      COUNT(m.id) as matches,\n      SUM(CASE\n          WHEN (m.home = t.id AND m.home_cups > m.away_cups) OR\n               (m.away = t.id AND m.away_cups > m.home_cups)\n          THEN 1 ELSE 0\n      END) as wins,\n      SUM(CASE\n          WHEN (m.home = t.id AND m.home_cups < m.away_cups) OR\n               (m.away = t.id AND m.away_cups < m.home_cups)\n          THEN 1 ELSE 0\n      END) as losses,\n      SUM(CASE\n          WHEN m.home = t.id THEN m.home_cups\n          WHEN m.away = t.id THEN m.away_cups\n          ELSE 0\n      END) as total_cups_made,\n      SUM(CASE\n          WHEN m.home = t.id THEN m.away_cups\n          WHEN m.away = t.id THEN m.home_cups\n          ELSE 0\n      END) as total_cups_against,\n      -- Win percentage\n      ROUND((CAST(SUM(CASE\n          WHEN (m.home = t.id AND m.home_cups > m.away_cups) OR\n               (m.away = t.id AND m.away_cups > m.home_cups)\n          THEN 1 ELSE 0\n      END) AS REAL) / COUNT(m.id)) * 100, 2) as win_percentage,\n      -- Average cups per match\n      ROUND(CAST(SUM(CASE\n          WHEN m.home = t.id THEN m.home_cups\n          WHEN m.away = t.id THEN m.away_cups\n          ELSE 0\n      END) AS REAL) / COUNT(m.id), 2) as avg_cups_per_match,\n      -- Margin of Victory\n      ROUND(AVG(CASE\n          WHEN m.home = t.id AND m.home_cups > m.away_cups\n          THEN m.home_cups - m.away_cups\n          WHEN m.away = t.id AND m.away_cups > m.home_cups\n          THEN m.away_cups - m.home_cups\n          ELSE NULL\n      END), 2) as margin_of_victory,\n      -- Margin of Loss\n      ROUND(AVG(CASE\n          WHEN m.home = t.id AND m.home_cups < m.away_cups\n          THEN m.away_cups - m.home_cups\n          WHEN m.away = t.id AND m.away_cups < m.home_cups\n          THEN m.home_cups - m.away_cups\n          ELSE NULL\n      END), 2) as margin_of_loss\n  FROM players p, teams t, matches m, tournaments tour\n  WHERE\n      t.players LIKE '%\"' || p.id || '\"%' AND\n      (m.home = t.id OR m.away = t.id) AND\n      m.tournament = tour.id AND\n      m.status = 'ended'\n  GROUP BY p.id"
  }, collection)

  // remove field
  collection.fields.removeById("relation694999214")

  // remove field
  collection.fields.removeById("_clone_fQu1")

  return app.save(collection)
}, (app) => {
  const collection = app.findCollectionByNameOrId("pbc_1358894712")

  // update collection data
  unmarshal({
    "viewQuery": "SELECT\n        (p.id || '_' || t.id) as id,\n        p.id as player_id,\n        (p.first_name || ' ' || p.last_name) as player_name,\n        t.id as team_id,\n        t.name as team_name,\n        COUNT(m.id) as matches,\n        SUM(CASE\n            WHEN (m.home = t.id AND m.home_cups > m.away_cups) OR\n                 (m.away = t.id AND m.away_cups > m.home_cups)\n            THEN 1 ELSE 0\n        END) as wins,\n        SUM(CASE\n            WHEN (m.home = t.id AND m.home_cups < m.away_cups) OR\n                 (m.away = t.id AND m.away_cups < m.home_cups)\n            THEN 1 ELSE 0\n        END) as losses,\n        SUM(CASE\n            WHEN m.home = t.id THEN m.home_cups\n            WHEN m.away = t.id THEN m.away_cups\n            ELSE 0\n        END) as total_cups_made,\n        SUM(CASE\n            WHEN m.home = t.id THEN m.away_cups\n            WHEN m.away = t.id THEN m.home_cups\n            ELSE 0\n        END) as total_cups_against,\n        -- Additional calculated stats\n        ROUND((CAST(SUM(CASE\n            WHEN (m.home = t.id AND m.home_cups > m.away_cups) OR\n                 (m.away = t.id AND m.away_cups > m.home_cups)\n            THEN 1 ELSE 0\n        END) AS REAL) / COUNT(m.id)) * 100, 2) as win_percentage,\n        ROUND(CAST(SUM(CASE\n            WHEN m.home = t.id THEN m.home_cups\n            WHEN m.away = t.id THEN m.away_cups\n            ELSE 0\n        END) AS REAL) / COUNT(m.id), 2) as avg_cups_per_match,\n        -- Margin calculations\n        AVG(CASE\n            WHEN m.home = t.id AND m.home_cups > m.away_cups\n            THEN m.home_cups - m.away_cups\n            WHEN m.away = t.id AND m.away_cups > m.home_cups\n            THEN m.away_cups - m.home_cups\n            ELSE NULL\n        END) as margin_of_victory,\n        AVG(CASE\n            WHEN m.home = t.id AND m.home_cups < m.away_cups\n            THEN m.away_cups - m.home_cups\n            WHEN m.away = t.id AND m.away_cups < m.home_cups\n            THEN m.home_cups - m.away_cups\n            ELSE NULL\n        END) as margin_of_loss\n    FROM players p, teams t, matches m, tournaments tour\n    WHERE\n        t.players LIKE '%\"' || p.id || '\"%' AND\n        (m.home = t.id OR m.away = t.id) AND\n        m.tournament = tour.id AND\n        m.status = 'ended'\n    GROUP BY p.id, t.id"
  }, collection)

  // add field
  collection.fields.addAt(3, new Field({
    "cascadeDelete": false,
    "collectionId": "pbc_1568971955",
    "hidden": false,
    "id": "relation694999214",
    "maxSelect": 1,
    "minSelect": 0,
    "name": "team_id",
    "presentable": false,
    "required": false,
    "system": false,
    "type": "relation"
  }))

  // add field
  collection.fields.addAt(4, new Field({
    "autogeneratePattern": "",
    "hidden": false,
    "id": "_clone_fQu1",
    "max": 0,
    "min": 0,
    "name": "team_name",
    "pattern": "",
    "presentable": false,
    "primaryKey": false,
    "required": true,
    "system": false,
    "type": "text"
  }))

  return app.save(collection)
})
