{
  "$schema": "./.schemas/page.schema.json",
  "title": "Shopping list",
  "icon": "cart",
  "sidebar": false,
  "params": {
    "list": {
      "example": "0f6a3c1e-5b7d-4e2a-9c81-3d5f7a2b4c60",
      "pattern": "[0-9a-fA-F-]{36}"
    }
  },
  "title_from": {
    "ref": "mealie",
    "value": "list.name"
  },
  "sources": {
    "mealie": {
      "url": "http://mealie.local:9925",
      "secret": "mealie",
      "auth": {
        "type": "bearer"
      },
      "every": "5m",
      "requests": {
        "list": "/api/households/shopping/lists/{param.list}",
        "items": "/api/households/shopping/items?queryFilter=shoppingListId%3D%22{param.list}%22&orderBy=checked&orderDirection=asc&perPage=200"
      },
      "derive": [
        {
          "name": "selected_id",
          "value": "list.id"
        },
        {
          "name": "aisles",
          "value": {
            "each": "list.labelSettings",
            "fields": {
              "labelId": "labelId",
              "rank": {
                "path": "position",
                "default": 999
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "shopping",
      "type": "checklist",
      "label": "To buy",
      "items": [],
      "max_items": 15,
      "source": {
        "ref": "mealie",
        "map": {
          "items": {
            "each": "items.items",
            "lookup": {
              "from": "aisles",
              "by": "labelId",
              "on": "labelId",
              "as": "aisle"
            },
            "where": {
              "path": "shoppingListId",
              "equals": {
                "answer": "selected_id"
              }
            },
            "sort": [
              "checked",
              "aisle.rank",
              "position"
            ],
            "limit": 200,
            "fields": {
              "text": {
                "if": {
                  "path": "display",
                  "not": ""
                },
                "then": {
                  "template": "{display|clip:120}"
                },
                "else": {
                  "if": {
                    "path": "note",
                    "exists": true
                  },
                  "then": {
                    "template": "{note|clip:120}"
                  },
                  "else": {
                    "template": "Item"
                  }
                }
              },
              "done": "checked",
              "section": {
                "if": {
                  "path": "checked",
                  "equals": true
                },
                "then": {
                  "template": "Ticked"
                },
                "else": {
                  "if": {
                    "path": "label.name",
                    "exists": true
                  },
                  "then": {
                    "template": "{label.name|clip:40}"
                  },
                  "else": {
                    "template": "Other"
                  }
                }
              }
            }
          }
        },
        "empty": {
          "text": "Nothing on this list.",
          "when": {
            "all": [
              {
                "value": {
                  "count": "items.items",
                  "where": {
                    "path": "shoppingListId",
                    "equals": {
                      "answer": "selected_id"
                    }
                  }
                },
                "equals": 0
              },
              {
                "value": {
                  "path": "items.total",
                  "default": 0
                },
                "lte": 200
              }
            ]
          }
        },
        "when": {
          "path": "selected_id",
          "exists": true
        }
      },
      "itemAction": {
        "label": "Tick",
        "icon": "checkmark",
        "method": "PUT",
        "path": "/api/households/shopping/items/{id}",
        "body_from": "row",
        "body": {
          "checked": true
        }
      },
      "actions": [
        {
          "label": "Add an item",
          "icon": "plus",
          "method": "POST",
          "path": "/api/households/shopping/items",
          "body": {
            "shoppingListId": "{answer.selected_id}",
            "note": "{input.item}"
          },
          "inputs": [
            {
              "name": "item",
              "label": "Item",
              "required": true,
              "max": 120,
              "hint": "2 lemons"
            }
          ],
          "when": {
            "path": "selected_id",
            "exists": true
          }
        }
      ],
      "actions_placement": "corner"
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "Legato reads 200 items on this list, the unticked ones first. Open [Mealie](http://mealie.local:9925) for the rest.",
      "source": {
        "ref": "mealie",
        "when": {
          "value": {
            "path": "items.total",
            "default": 0
          },
          "gt": 200
        }
      }
    }
  ]
}
