{
  "$schema": "./.schemas/page.schema.json",
  "title": "Upcoming",
  "icon": "calendar.badge.clock",
  "sources": {
    "recurring": {
      "derive": [
        {
          "name": "bills_left",
          "value": "summary.totals.outflow.recurring_remaining"
        },
        {
          "name": "income_left",
          "value": {
            "path": "summary.totals.inflow.recurring_remaining",
            "scale": -1
          }
        },
        {
          "name": "rules",
          "value": {
            "each": "week.recurring_items",
            "where": {
              "path": "status",
              "equals": "reviewed"
            },
            "fields": {
              "id": "id",
              "amount": "transaction_criteria.to_base",
              "income": {
                "multiply": [
                  "transaction_criteria.to_base",
                  -1
                ]
              },
              "payee": {
                "path": "overrides.payee",
                "default": {
                  "template": "{transaction_criteria.payee}"
                }
              },
              "dates": {
                "each": "matches.missing_transaction_dates",
                "fields": {
                  "date": "$"
                }
              }
            }
          }
        },
        {
          "name": "upcoming",
          "value": {
            "each": "rules",
            "flatten": "dates",
            "sort": "date",
            "fields": {
              "key": {
                "template": "{parent.id}:{date}"
              },
              "amount": "parent.amount",
              "income": "parent.income",
              "payee": "parent.payee",
              "start": "date"
            }
          }
        },
        {
          "name": "occurrence_count",
          "value": {
            "count": "upcoming"
          }
        }
      ],
      "every": "1h",
      "requests": {
        "summary": "/v2/summary?start_date={date.month_start}&end_date={date.month_end}&include_totals=true",
        "week": "/v2/recurring_items?start_date={date.today}&end_date={date.days_ahead_29}&include_suggested=false",
        "me": "/v2/me"
      },
      "secret": "lunch-money",
      "url": "https://api.lunchmoney.dev"
    }
  },
  "cards": [
    {
      "id": "bills-left",
      "label": "Expected expenses left",
      "source": {
        "map": {
          "delta": {
            "template": "this month"
          },
          "value": {
            "path": "bills_left",
            "round": 0
          },
          "unit": "me.primary_currency"
        },
        "ref": "recurring"
      },
      "type": "stat",
      "unit": "USD",
      "value": 0
    },
    {
      "id": "income-left",
      "label": "Expected income left",
      "source": {
        "map": {
          "delta": {
            "template": "this month"
          },
          "value": {
            "path": "income_left",
            "round": 0
          },
          "unit": "me.primary_currency"
        },
        "ref": "recurring"
      },
      "type": "stat",
      "unit": "USD",
      "value": 0
    },
    {
      "id": "paid",
      "label": "Matched recurring expenses",
      "source": {
        "map": {
          "target": {
            "path": "summary.totals.outflow.recurring_expected",
            "round": 0
          },
          "value": {
            "path": "summary.totals.outflow.recurring_activity",
            "round": 0
          },
          "unit": "me.primary_currency"
        },
        "ref": "recurring"
      },
      "target": 1,
      "type": "pacing",
      "unit": "USD",
      "value": 0
    },
    {
      "id": "horizons",
      "type": "tabs",
      "tabs": [
        {
          "label": "Next 7 days",
          "cards": [
            {
              "days": 7,
              "hide_all_day_label": true,
              "hide_empty_days": true,
              "id": "next-seven",
              "items": [],
              "source": {
                "map": {
                  "items": {
                    "each": "upcoming",
                    "fields": {
                      "detail": {
                        "if": {
                          "path": "amount",
                          "lt": 0
                        },
                        "then": {
                          "template": "+${income|round:2} expected"
                        },
                        "else": {
                          "template": "${amount|round:2} expected"
                        }
                      },
                      "start": "start",
                      "title": "payee"
                    },
                    "limit": 60
                  }
                },
                "ref": "recurring"
              },
              "type": "agenda",
              "max_events": 8
            }
          ]
        },
        {
          "label": "Next 30 days",
          "cards": [
            {
              "days": 30,
              "hide_all_day_label": true,
              "hide_empty_days": true,
              "id": "week",
              "items": [],
              "source": {
                "map": {
                  "items": {
                    "each": "upcoming",
                    "fields": {
                      "detail": {
                        "if": {
                          "path": "amount",
                          "lt": 0
                        },
                        "then": {
                          "template": "+${income|round:2} expected"
                        },
                        "else": {
                          "template": "${amount|round:2} expected"
                        }
                      },
                      "start": "start",
                      "title": "payee"
                    },
                    "limit": 60
                  }
                },
                "ref": "recurring"
              },
              "type": "agenda",
              "max_events": 8
            }
          ]
        }
      ]
    }
  ]
}
