{
  "$schema": "./.schemas/page.schema.json",
  "title": "Budget",
  "icon": "chart.pie",
  "period": {
    "step": true,
    "choices": [
      {
        "id": "month",
        "label": "Month",
        "span": "month"
      }
    ]
  },
  "sources": {
    "month": {
      "url": "https://api.ynab.com",
      "secret": "ynab",
      "every": "15m",
      "requests": {
        "month": "/v1/plans/last-used/months/{period.start}",
        "settings": "/v1/plans/last-used/settings"
      },
      "derive": [
        {
          "name": "cats",
          "value": {
            "each": "month.data.month.categories",
            "where": {
              "all": [
                {
                  "path": "hidden",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "internal",
                  "not": true
                }
              ]
            },
            "fields": {
              "id": "id",
              "name": "name",
              "group": {
                "path": "category_group_name",
                "default": ""
              },
              "available": "balance_currency",
              "available_text": "balance_formatted",
              "spent": {
                "multiply": [
                  "activity_currency",
                  -1
                ]
              },
              "funded": {
                "subtract": [
                  "balance_currency",
                  "activity_currency"
                ]
              },
              "under": {
                "path": "goal_under_funded_currency",
                "default": 0
              },
              "snoozed": {
                "path": "goal_snoozed_at",
                "default": null
              },
              "overspent": {
                "if": {
                  "path": "balance_currency",
                  "lt": 0
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "0"
                }
              }
            }
          }
        },
        {
          "name": "underfunded",
          "value": {
            "count": "cats",
            "where": {
              "all": [
                {
                  "path": "under",
                  "gt": 0
                },
                {
                  "path": "snoozed",
                  "exists": false
                }
              ]
            }
          }
        },
        {
          "name": "overspent",
          "value": {
            "count": "cats",
            "where": {
              "path": "available",
              "lt": 0
            }
          }
        }
      ]
    },
    "spend": {
      "url": "https://api.ynab.com",
      "secret": "ynab",
      "every": "30m",
      "requests": {
        "month": "/v1/plans/last-used/months/{period.start}",
        "tx": "/v1/plans/last-used/months/{period.start}/transactions?since_date={period.start}",
        "settings": "/v1/plans/last-used/settings"
      },
      "derive": [
        {
          "name": "cats",
          "value": {
            "each": "month.data.month.categories",
            "where": {
              "all": [
                {
                  "path": "hidden",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "internal",
                  "not": true
                }
              ]
            },
            "fields": {
              "id": "id",
              "name": "name",
              "group": {
                "path": "category_group_name",
                "default": ""
              },
              "available": "balance_currency",
              "available_text": "balance_formatted",
              "spent": {
                "multiply": [
                  "activity_currency",
                  -1
                ]
              },
              "funded": {
                "subtract": [
                  "balance_currency",
                  "activity_currency"
                ]
              },
              "under": {
                "path": "goal_under_funded_currency",
                "default": 0
              },
              "snoozed": {
                "path": "goal_snoozed_at",
                "default": null
              },
              "overspent": {
                "if": {
                  "path": "balance_currency",
                  "lt": 0
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "0"
                }
              }
            }
          }
        },
        {
          "name": "spend_rows",
          "value": {
            "each": "cats",
            "where": {
              "all": [
                {
                  "path": "funded",
                  "gt": 0
                },
                {
                  "path": "spent",
                  "gte": 0
                },
                {
                  "path": "group",
                  "not": "Credit Card Payments"
                }
              ]
            },
            "fields": {
              "id": "id",
              "name": "name",
              "group": "group",
              "spent": "spent",
              "funded": "funded",
              "available_text": "available_text",
              "overspent": "overspent",
              "used": {
                "divide": [
                  "spent",
                  "funded"
                ],
                "default": 0
              }
            }
          }
        },
        {
          "name": "spend_count",
          "value": {
            "count": "spend_rows"
          }
        },
        {
          "name": "unfunded",
          "value": {
            "each": "cats",
            "where": {
              "all": [
                {
                  "path": "available",
                  "lt": 0
                },
                {
                  "path": "funded",
                  "lte": 0
                }
              ]
            },
            "fields": {
              "name": "name",
              "available_text": "available_text",
              "available": "available"
            }
          }
        },
        {
          "name": "unfunded_count",
          "value": {
            "count": "unfunded"
          }
        },
        {
          "name": "whole",
          "value": {
            "each": "tx.data.transactions",
            "where": {
              "all": [
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "category_id",
                  "exists": true
                },
                {
                  "path": "category_name",
                  "not": "Split"
                }
              ]
            },
            "fields": {
              "key": "category_id",
              "date": "date",
              "payee": {
                "if": {
                  "path": "payee_name",
                  "exists": true
                },
                "then": {
                  "path": "payee_name"
                },
                "else": {
                  "if": {
                    "path": "import_payee_name_original",
                    "exists": true
                  },
                  "then": {
                    "path": "import_payee_name_original"
                  },
                  "else": {
                    "template": "No payee"
                  }
                }
              },
              "amount_text": "amount_formatted"
            }
          }
        },
        {
          "name": "pieces",
          "value": {
            "each": "tx.data.transactions",
            "where": {
              "path": "deleted",
              "not": true
            },
            "flatten": "subtransactions",
            "fields": {
              "key": {
                "path": "category_id",
                "default": null
              },
              "deleted": {
                "path": "deleted",
                "default": false
              },
              "date": "parent.date",
              "payee": {
                "if": {
                  "path": "payee_name",
                  "exists": true
                },
                "then": {
                  "path": "payee_name"
                },
                "else": {
                  "if": {
                    "path": "parent.payee_name",
                    "exists": true
                  },
                  "then": {
                    "path": "parent.payee_name"
                  },
                  "else": {
                    "if": {
                      "path": "parent.import_payee_name_original",
                      "exists": true
                    },
                    "then": {
                      "path": "parent.import_payee_name_original"
                    },
                    "else": {
                      "template": "Split"
                    }
                  }
                }
              },
              "amount_text": "amount_formatted"
            }
          }
        },
        {
          "name": "detail_rows",
          "value": {
            "each": [
              "whole",
              "pieces"
            ],
            "where": {
              "all": [
                {
                  "path": "key",
                  "exists": true
                },
                {
                  "path": "deleted",
                  "not": true
                }
              ]
            },
            "fields": {
              "key": "key",
              "date": "date",
              "payee": "payee",
              "amount_text": "amount_text"
            }
          }
        },
        {
          "name": "detail_count",
          "value": {
            "count": "detail_rows"
          }
        },
        {
          "name": "details",
          "value": {
            "each": "detail_rows",
            "sort": {
              "by": "date",
              "order": "desc"
            },
            "limit": 500,
            "fields": {
              "key": "key",
              "text": "payee",
              "detail": {
                "template": "{amount_text} · {date|date}"
              }
            }
          }
        }
      ]
    },
    "review": {
      "url": "https://api.ynab.com",
      "secret": "ynab",
      "every": "15m",
      "requests": {
        "unapproved": "/v1/plans/last-used/transactions?type=unapproved&since_date=2000-01-01"
      },
      "derive": [
        {
          "name": "waiting",
          "value": {
            "count": "unapproved.data.transactions",
            "where": {
              "all": [
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "approved",
                  "equals": false
                }
              ]
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "assign",
      "type": "stat",
      "label": "Ready to Assign",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "month",
        "map": {
          "value": "month.data.month.to_be_budgeted_currency",
          "unit": "settings.data.settings.currency_format.iso_code"
        }
      }
    },
    {
      "id": "age",
      "type": "stat",
      "label": "Age of Money",
      "unit": "days",
      "value": 0,
      "source": {
        "ref": "month",
        "map": {
          "value": "month.data.month.age_of_money"
        }
      }
    },
    {
      "id": "month-facts",
      "type": "facts",
      "items": [
        {
          "key": "Assigned",
          "value": "$0.00"
        },
        {
          "key": "Activity",
          "value": "$0.00"
        },
        {
          "key": "Income",
          "value": "$0.00"
        },
        {
          "key": "Overspent",
          "value": "None"
        },
        {
          "key": "Underfunded",
          "value": "None"
        }
      ],
      "source": {
        "ref": "month",
        "map": {
          "items[0].value": "month.data.month.budgeted_formatted",
          "items[1].value": "month.data.month.activity_formatted",
          "items[2].value": "month.data.month.income_formatted",
          "items[3].value": {
            "path": "overspent",
            "values": {
              "0": "None",
              "1": "1 category"
            },
            "default": {
              "template": "{overspent} categories"
            }
          },
          "items[4].value": {
            "path": "underfunded",
            "values": {
              "0": "None",
              "1": "1 category"
            },
            "default": {
              "template": "{underfunded} categories"
            }
          }
        }
      }
    },
    {
      "id": "categories",
      "type": "pacing",
      "label": "Categories",
      "unit": "USD",
      "items": [
        {
          "label": "Category",
          "value": 0,
          "target": 1
        }
      ],
      "source": {
        "ref": "spend",
        "empty": {
          "when": {
            "path": "spend_count",
            "equals": 0
          },
          "text": "No spending from a funded category"
        },
        "map": {
          "items": {
            "each": "spend_rows",
            "sort": {
              "by": [
                "overspent",
                "used"
              ],
              "order": "desc"
            },
            "limit": 20,
            "fields": {
              "key": "id",
              "label": "name",
              "value": "spent",
              "target": "funded",
              "state": {
                "path": "overspent",
                "values": {
                  "1": "Overspent"
                }
              }
            }
          },
          "details": "details",
          "unit": "settings.data.settings.currency_format.iso_code"
        }
      }
    },
    {
      "id": "more-categories",
      "type": "text",
      "tone": "info",
      "body": "Showing 20 of your categories: overspent ones, then those that have used the most of their money. See the rest in YNAB.",
      "source": {
        "ref": "spend",
        "when": {
          "path": "spend_count",
          "gt": 20
        },
        "map": {
          "body": {
            "template": "Showing 20 of {spend_count} categories: overspent ones, then those that have used the most of their money. See the rest in YNAB."
          }
        }
      }
    },
    {
      "id": "more-details",
      "type": "text",
      "tone": "info",
      "body": "The categories list the newest 500 transactions in the month. See the rest in YNAB.",
      "source": {
        "ref": "spend",
        "when": {
          "path": "detail_count",
          "gt": 500
        },
        "map": {
          "body": {
            "template": "The categories list the newest 500 of {detail_count} transactions in the month. See the rest in YNAB."
          }
        }
      }
    },
    {
      "id": "unfunded",
      "type": "list",
      "label": "Overspent with nothing assigned",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "spend",
        "when": {
          "path": "unfunded_count",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "unfunded",
            "sort": "available",
            "limit": 60,
            "fields": {
              "text": "name",
              "detail": "available_text"
            }
          }
        }
      }
    },
    {
      "id": "more-unfunded",
      "type": "text",
      "tone": "info",
      "body": "Showing 60 overspent categories with nothing assigned. See the rest in YNAB.",
      "source": {
        "ref": "spend",
        "when": {
          "path": "unfunded_count",
          "gt": 60
        },
        "map": {
          "body": {
            "template": "Showing 60 of {unfunded_count} overspent categories with nothing assigned. See the rest in YNAB."
          }
        }
      }
    },
    {
      "id": "approve",
      "type": "list",
      "label": "Needs approval",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "review",
        "empty": {
          "when": {
            "path": "waiting",
            "equals": 0
          },
          "text": "Nothing to approve"
        },
        "map": {
          "items": {
            "each": "unapproved.data.transactions",
            "where": {
              "all": [
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "approved",
                  "equals": false
                }
              ]
            },
            "sort": {
              "by": "date",
              "order": "desc"
            },
            "limit": 60,
            "fields": {
              "key": "id",
              "text": {
                "if": {
                  "path": "payee_name",
                  "exists": true
                },
                "then": {
                  "path": "payee_name"
                },
                "else": {
                  "if": {
                    "path": "import_payee_name_original",
                    "exists": true
                  },
                  "then": {
                    "path": "import_payee_name_original"
                  },
                  "else": {
                    "template": "No payee"
                  }
                }
              },
              "detail": {
                "template": "{amount_formatted} · {date|date}"
              }
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Approve",
          "icon": "checkmark.circle",
          "method": "PUT",
          "path": "/v1/plans/last-used/transactions/{id}",
          "body": {
            "transaction": {
              "approved": true
            }
          },
          "confirm": true,
          "when": {
            "path": "id",
            "exists": true
          }
        }
      ]
    },
    {
      "id": "more-approvals",
      "type": "text",
      "tone": "info",
      "body": "Showing the newest 60 waiting for approval. Approve the rest in YNAB.",
      "source": {
        "ref": "review",
        "when": {
          "path": "waiting",
          "gt": 60
        },
        "map": {
          "body": {
            "template": "Showing the newest 60 of {waiting} waiting for approval. Approve the rest in YNAB."
          }
        }
      }
    },
    {
      "id": "explore",
      "type": "list",
      "label": "Explore",
      "items": [
        {
          "text": "Accounts",
          "url": "legato://page/ynab-accounts"
        },
        {
          "text": "Upcoming",
          "url": "legato://page/ynab-upcoming"
        },
        {
          "text": "Open YNAB",
          "detail": "app.ynab.com",
          "url": "https://app.ynab.com"
        }
      ]
    }
  ]
}
