/// <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_3072146508",
        "hidden": false,
        "id": "relation2582050271",
        "maxSelect": 1,
        "minSelect": 0,
        "name": "player_id",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "relation"
      },
      {
        "hidden": false,
        "id": "json4231605813",
        "maxSize": 1,
        "name": "player_name",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "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_fQu1",
        "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": "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"
      },
      {
        "hidden": false,
        "id": "json2379943496",
        "maxSize": 1,
        "name": "win_percentage",
        "presentable": false,
        "required": false,
        "system": false,
        "type": "json"
      },
      {
        "hidden": false,
        "id": "json3165107022",
        "maxSize": 1,
        "name": "avg_cups_per_match",
        "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"
      }
    ],
    "id": "pbc_1358894712",
    "indexes": [],
    "listRule": null,
    "name": "player_stats",
    "system": false,
    "type": "view",
    "updateRule": null,
    "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",
    "viewRule": null
  });

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

  return app.delete(collection);
})
