/// <reference path="../pb_data/types.d.ts" />
migrate((app) => {
  const collection = new Collection({
    "createRule": null,
    "deleteRule": null,
    "fields": [
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "text3208210256",
        "max": 0,
        "min": 0,
        "name": "id",
        "pattern": "^[a-z0-9]+$",
        "presentable": false,
        "primaryKey": true,
        "required": true,
        "system": true,
        "type": "text"
      },
      {
        "cascadeDelete": false,
        "collectionId": "pbc_1568971955",
        "hidden": false,
        "id": "relation694999214",
        "maxSelect": 1,
        "minSelect": 0,
        "name": "team_id",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "relation"
      },
      {
        "autogeneratePattern": "",
        "hidden": false,
        "id": "_clone_ZNMy",
        "max": 0,
        "min": 0,
        "name": "team_name",
        "pattern": "",
        "presentable": false,
        "primaryKey": false,
        "required": true,
        "system": false,
        "type": "text"
      },
      {
        "hidden": false,
        "id": "number103159226",
        "max": null,
        "min": null,
        "name": "matches",
        "onlyInt": false,
        "presentable": false,
        "required": false,
        "system": false,
        "type": "number"
      },
      {
        "hidden": false,
        "id": "json2732118329",
        "maxSize": 1,
        "name": "wins",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json724428801",
        "maxSize": 1,
        "name": "losses",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json3041953980",
        "maxSize": 1,
        "name": "margin_of_victory",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json1531431708",
        "maxSize": 1,
        "name": "margin_of_loss",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json1062535948",
        "maxSize": 1,
        "name": "total_cups_won_by",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json4249694556",
        "maxSize": 1,
        "name": "total_cups_lost_by",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json3154249934",
        "maxSize": 1,
        "name": "total_cups_made",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json3227208027",
        "maxSize": 1,
        "name": "total_cups_against",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      }
    ],
    "id": "pbc_135889472",
    "indexes": [],
    "listRule": null,
    "name": "team_stats",
    "system": false,
    "type": "view",
    "updateRule": null,
    "viewQuery": `
  SELECT
      t.id as id,
      t.id as team_id,
      t.name as team_name,
      COUNT(m.id) as matches,
      SUM(CASE
          WHEN (m.home = t.id AND m.home_cups > m.away_cups) OR
               (m.away = t.id AND m.away_cups > m.home_cups)
          THEN 1 ELSE 0
      END) as wins,
      SUM(CASE
          WHEN (m.home = t.id AND m.home_cups < m.away_cups) OR
               (m.away = t.id AND m.away_cups < m.home_cups)
          THEN 1 ELSE 0
      END) as losses,
      AVG(CASE
          WHEN m.home = t.id AND m.home_cups > m.away_cups
          THEN m.home_cups - m.away_cups
          WHEN m.away = t.id AND m.away_cups > m.home_cups
          THEN m.away_cups - m.home_cups
          ELSE NULL
      END) as margin_of_victory,
      AVG(CASE
          WHEN m.home = t.id AND m.home_cups < m.away_cups
          THEN m.away_cups - m.home_cups
          WHEN m.away = t.id AND m.away_cups < m.home_cups
          THEN m.home_cups - m.away_cups
          ELSE NULL
      END) as margin_of_loss,
      SUM(CASE
          WHEN m.home = t.id THEN m.home_cups
          WHEN m.away = t.id THEN m.away_cups
          ELSE 0
      END) as total_cups_won_by,
      SUM(CASE
          WHEN m.home = t.id THEN m.away_cups
          WHEN m.away = t.id THEN m.home_cups
          ELSE 0
      END) as total_cups_lost_by,
      SUM(CASE
          WHEN m.home = t.id THEN m.home_cups
          WHEN m.away = t.id THEN m.away_cups
          ELSE 0
      END) as total_cups_made,
      SUM(CASE
          WHEN m.home = t.id THEN m.away_cups
          WHEN m.away = t.id THEN m.home_cups
          ELSE 0
      END) as total_cups_against
  FROM teams t
  JOIN matches m ON (m.home = t.id OR m.away = t.id)
  JOIN tournaments tour ON m.tournament = tour.id
  WHERE m.status = 'ended'
  GROUP BY t.id`,
    "viewRule": null
  });

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

  return app.delete(collection);
})