{
  "$schema": "./.schemas/page.schema.json",
  "title": "Mealie",
  "icon": "fork.knife",
  "sources": {
    "mealie": {
      "url": "http://mealie.local:9925",
      "secret": "mealie",
      "auth": {
        "type": "bearer"
      },
      "every": "5m",
      "requests": {
        "plan": "/api/households/mealplans?start_date={date.today}&end_date={date.days_ahead_6}&include_week_entries=true&orderBy=date&orderDirection=asc&perPage=100",
        "items": "/api/households/shopping/items?orderBy=checked&orderDirection=asc&perPage=200",
        "lists": "/api/households/shopping/lists?orderBy=name&orderDirection=asc&perPage=100",
        "made": "/api/recipes/timeline/events?queryFilter=eventType%3D%22comment%22&orderBy=timestamp&orderDirection=desc&perPage=4",
        "made_recipes": {
          "path": "/api/recipes/{recipeId}",
          "each_row": {
            "from": "made.items",
            "key": "id",
            "max": 4,
            "pick": [
              "id",
              "name",
              "slug",
              "image",
              "groupId"
            ],
            "keep": "6h"
          }
        },
        "group": "/api/groups/self",
        "stats": "/api/households/statistics",
        "about": "/api/app/about"
      },
      "derive": [
        {
          "name": "meals",
          "value": {
            "each": "plan.items",
            "lookup": {
              "from": "group",
              "by": "id",
              "on": "groupId",
              "as": "grp"
            },
            "fields": {
              "id": "id",
              "date": "date",
              "week": {
                "if": {
                  "path": "endDate",
                  "exists": true
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              },
              "order": {
                "if": {
                  "path": "endDate",
                  "exists": true
                },
                "then": {
                  "template": "9999-{date}"
                },
                "else": "date"
              },
              "rank": {
                "path": "entryType",
                "values": {
                  "breakfast": 0,
                  "lunch": 1,
                  "dinner": 2,
                  "side": 3,
                  "dessert": 4,
                  "snack": 5,
                  "drink": 6
                },
                "default": 7
              },
              "meal": {
                "path": "entryType",
                "values": {
                  "breakfast": "Breakfast",
                  "lunch": "Lunch",
                  "dinner": "Dinner",
                  "side": "Side",
                  "dessert": "Dessert",
                  "snack": "Snack",
                  "drink": "Drink"
                },
                "default": "Meal"
              },
              "text": {
                "if": {
                  "path": "recipe.name",
                  "exists": true
                },
                "then": "recipe.name",
                "else": {
                  "path": "title",
                  "default": "Note"
                }
              },
              "subtitle": {
                "if": {
                  "path": "recipe.totalTime",
                  "exists": true
                },
                "then": "recipe.totalTime",
                "else": {
                  "path": "text",
                  "default": ""
                }
              },
              "icon": {
                "if": {
                  "path": "recipe.id",
                  "exists": true
                },
                "then": {
                  "template": "fork.knife"
                },
                "else": {
                  "template": "note.text"
                }
              },
              "image": {
                "if": {
                  "path": "recipe.image",
                  "exists": true
                },
                "then": {
                  "template": "/api/media/recipes/{recipe.id|raw}/images/min-original.webp"
                },
                "else": {
                  "template": ""
                }
              },
              "url": {
                "if": {
                  "all": [
                    {
                      "path": "recipe.slug",
                      "exists": true
                    },
                    {
                      "path": "grp.slug",
                      "exists": true
                    }
                  ]
                },
                "then": {
                  "template": "http://mealie.local:9925/g/{grp.slug|raw}/r/{recipe.slug|raw}"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "selected_id",
          "value": {
            "first": "id",
            "of": "lists.items",
            "where": {
              "path": "name",
              "equals": "Weekly shop"
            }
          }
        },
        {
          "name": "selected",
          "value": {
            "each": "lists.items",
            "where": {
              "path": "name",
              "equals": "Weekly shop"
            },
            "limit": 1,
            "fields": {
              "labelSettings": "labelSettings"
            }
          }
        },
        {
          "name": "aisles",
          "value": {
            "each": "selected[0].labelSettings",
            "fields": {
              "labelId": "labelId",
              "rank": {
                "path": "position",
                "default": 999
              }
            }
          }
        },
        {
          "name": "made_rows",
          "value": {
            "each": "made.items",
            "limit": 4,
            "lookup": {
              "from": "made_recipes",
              "by": "key",
              "on": "id",
              "as": "made"
            },
            "fields": {
              "id": "id",
              "groupId": "groupId",
              "timestamp": "timestamp",
              "eventMessage": "eventMessage",
              "recipe": "made.answer"
            }
          }
        },
        {
          "name": "tallies",
          "value": {
            "each": "items.items",
            "where": {
              "path": "checked",
              "equals": false
            },
            "group": "shoppingListId",
            "fields": {
              "key": "key",
              "count": "count"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "plan",
      "type": "list",
      "label": "Meal plan",
      "items": [],
      "max_items": 7,
      "image_shape": "square",
      "source": {
        "ref": "mealie",
        "map": {
          "items": {
            "each": "meals",
            "sort": [
              "order",
              "rank"
            ],
            "limit": 60,
            "fields": {
              "key": "id",
              "text": {
                "template": "{text|clip:120}"
              },
              "detail": "meal",
              "subtitle": {
                "if": {
                  "path": "subtitle",
                  "not": ""
                },
                "then": {
                  "template": "{subtitle|clip:80}"
                }
              },
              "section": {
                "if": {
                  "path": "week",
                  "equals": "yes"
                },
                "then": {
                  "if": {
                    "value": {
                      "since": "date"
                    },
                    "gte": 0
                  },
                  "then": {
                    "template": "Any day this week"
                  },
                  "else": {
                    "template": "Any day next week"
                  }
                },
                "else": {
                  "template": "{date|day}"
                }
              },
              "icon": "icon",
              "image": "image",
              "url": {
                "if": {
                  "path": "url",
                  "not": ""
                },
                "then": "url"
              }
            }
          }
        },
        "empty": {
          "text": "Nothing planned for the next 7 days.",
          "when": {
            "value": {
              "count": "meals"
            },
            "equals": 0
          }
        }
      }
    },
    {
      "id": "more-meals",
      "type": "text",
      "tone": "info",
      "body": "More is planned this week than Legato shows here. Open [Mealie](http://mealie.local:9925) for the rest.",
      "source": {
        "ref": "mealie",
        "when": {
          "any": [
            {
              "value": {
                "path": "plan.total",
                "default": 0
              },
              "gt": 100
            },
            {
              "value": {
                "count": "meals"
              },
              "gt": 60
            }
          ]
        }
      }
    },
    {
      "id": "shopping",
      "type": "checklist",
      "label": "Weekly shop",
      "items": [],
      "max_items": 7,
      "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": "no-list",
      "type": "text",
      "tone": "info",
      "body": "Mealie has no shopping list called Weekly shop. Make one in Mealie, or set this page up again with another name.",
      "source": {
        "ref": "mealie",
        "when": {
          "all": [
            {
              "path": "selected_id",
              "exists": false
            },
            {
              "value": {
                "path": "lists.total",
                "default": 0
              },
              "lte": 100
            }
          ]
        }
      }
    },
    {
      "id": "many-lists",
      "type": "text",
      "tone": "info",
      "body": "Legato reads your first 100 shopping lists by name, and Weekly shop isn't among them. Open [Mealie](http://mealie.local:9925) for the rest.",
      "source": {
        "ref": "mealie",
        "when": {
          "all": [
            {
              "path": "selected_id",
              "exists": false
            },
            {
              "value": {
                "path": "lists.total",
                "default": 0
              },
              "gt": 100
            }
          ]
        }
      }
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "Legato reads 200 shopping items, 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
        }
      }
    },
    {
      "id": "lists",
      "type": "list",
      "label": "Shopping lists",
      "items": [],
      "max_items": 4,
      "source": {
        "ref": "mealie",
        "map": {
          "items": {
            "each": "lists.items",
            "limit": 60,
            "lookup": {
              "from": "tallies",
              "by": "key",
              "on": "id",
              "as": "tally"
            },
            "fields": {
              "key": "id",
              "text": {
                "if": {
                  "path": "name",
                  "exists": true
                },
                "then": {
                  "template": "{name|clip:120}"
                },
                "else": {
                  "template": "Shopping list"
                }
              },
              "detail": {
                "if": {
                  "path": "tally.count",
                  "exists": true
                },
                "then": {
                  "template": "{tally.count}"
                },
                "else": {
                  "template": "0"
                }
              },
              "url": {
                "template": "legato://page/mealie-list?list={id}"
              }
            }
          },
          "label": {
            "if": {
              "value": {
                "path": "lists.total",
                "default": 0
              },
              "gt": 60
            },
            "then": {
              "template": "Shopping lists · 60 of {lists.total}"
            },
            "else": {
              "template": "Shopping lists"
            }
          }
        },
        "when": {
          "all": [
            {
              "value": {
                "count": "lists.items"
              },
              "gte": 2
            },
            {
              "value": {
                "path": "items.total",
                "default": 0
              },
              "lte": 200
            }
          ]
        }
      }
    },
    {
      "id": "lists-partial",
      "type": "list",
      "label": "Shopping lists",
      "items": [],
      "max_items": 4,
      "source": {
        "ref": "mealie",
        "map": {
          "items": {
            "each": "lists.items",
            "limit": 60,
            "lookup": {
              "from": "tallies",
              "by": "key",
              "on": "id",
              "as": "tally"
            },
            "fields": {
              "key": "id",
              "text": {
                "if": {
                  "path": "name",
                  "exists": true
                },
                "then": {
                  "template": "{name|clip:120}"
                },
                "else": {
                  "template": "Shopping list"
                }
              },
              "detail": {
                "if": {
                  "path": "tally.count",
                  "exists": true
                },
                "then": {
                  "template": "At least {tally.count}"
                },
                "else": {
                  "template": ""
                }
              },
              "url": {
                "template": "legato://page/mealie-list?list={id}"
              }
            }
          },
          "label": {
            "if": {
              "value": {
                "path": "lists.total",
                "default": 0
              },
              "gt": 60
            },
            "then": {
              "template": "Shopping lists · 60 of {lists.total}"
            },
            "else": {
              "template": "Shopping lists"
            }
          }
        },
        "when": {
          "all": [
            {
              "value": {
                "count": "lists.items"
              },
              "gte": 2
            },
            {
              "value": {
                "path": "items.total",
                "default": 0
              },
              "gt": 200
            }
          ]
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Mealie",
      "items": [
        {
          "key": "Recipes",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "mealie",
        "map": {
          "items[0].value": "stats.totalRecipes",
          "items[1].value": "about.version"
        }
      }
    },
    {
      "id": "made",
      "type": "list",
      "label": "Made recently",
      "items": [],
      "max_items": 4,
      "image_shape": "square",
      "source": {
        "ref": "mealie",
        "map": {
          "items": {
            "each": "made_rows",
            "lookup": {
              "from": "group",
              "by": "id",
              "on": "groupId",
              "as": "grp"
            },
            "fields": {
              "key": "id",
              "text": {
                "if": {
                  "path": "recipe.name",
                  "exists": true
                },
                "then": {
                  "template": "{recipe.name|clip:120}"
                },
                "else": {
                  "template": "A recipe no longer in Mealie"
                }
              },
              "subtitle": {
                "if": {
                  "path": "eventMessage",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "eventMessage",
                    "not": ""
                  },
                  "then": {
                    "template": "{eventMessage|clip:80}"
                  }
                }
              },
              "detail": {
                "template": "{timestamp|ago}"
              },
              "icon": {
                "template": "fork.knife"
              },
              "image": {
                "if": {
                  "path": "recipe.image",
                  "exists": true
                },
                "then": {
                  "template": "/api/media/recipes/{recipe.id|raw}/images/min-original.webp"
                },
                "else": {
                  "template": ""
                }
              },
              "url": {
                "if": {
                  "all": [
                    {
                      "path": "recipe.slug",
                      "exists": true
                    },
                    {
                      "path": "grp.slug",
                      "exists": true
                    }
                  ]
                },
                "then": {
                  "template": "http://mealie.local:9925/g/{grp.slug|raw}/r/{recipe.slug|raw}"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        "empty": {
          "text": "Nothing marked as made yet.",
          "when": {
            "value": {
              "count": "made.items"
            },
            "equals": 0
          }
        }
      }
    }
  ]
}
