{
  "$schema": "./.schemas/page.schema.json",
  "title": "Upcoming",
  "icon": "calendar.badge.clock",
  "sources": {
    "scheduled": {
      "url": "https://api.ynab.com",
      "secret": "ynab",
      "every": "1h",
      "requests": {
        "scheduled": "/v1/plans/last-used/scheduled_transactions",
        "settings": "/v1/plans/last-used/settings"
      },
      "derive": [
        {
          "name": "ahead",
          "value": {
            "each": "scheduled.data.scheduled_transactions",
            "where": {
              "path": "deleted",
              "not": true
            },
            "fields": {
              "id": "id",
              "payee": {
                "if": {
                  "path": "payee_name",
                  "exists": true
                },
                "then": {
                  "path": "payee_name"
                },
                "else": {
                  "template": "No payee"
                }
              },
              "date": "date_next",
              "days": {
                "expires": {
                  "date": "date_next"
                },
                "as": "days"
              },
              "amount": "amount_currency",
              "outflow": {
                "multiply": [
                  "amount_currency",
                  -1
                ]
              },
              "amount_text": "amount_formatted",
              "account": {
                "path": "account_name",
                "default": ""
              },
              "often": {
                "path": "frequency",
                "values": {
                  "never": "Once",
                  "daily": "Daily",
                  "weekly": "Weekly",
                  "everyOtherWeek": "Every other week",
                  "twiceAMonth": "Twice a month",
                  "every4Weeks": "Every 4 weeks",
                  "monthly": "Monthly",
                  "everyOtherMonth": "Every other month",
                  "every3Months": "Every 3 months",
                  "every4Months": "Every 4 months",
                  "twiceAYear": "Twice a year",
                  "yearly": "Yearly",
                  "everyOtherYear": "Every other year"
                },
                "default": {
                  "template": "{frequency}"
                }
              },
              "frequent": {
                "path": "frequency",
                "values": {
                  "daily": "yes",
                  "weekly": "yes",
                  "everyOtherWeek": "yes",
                  "twiceAMonth": "yes",
                  "every4Weeks": "yes"
                },
                "default": "no"
              },
              "transfer": {
                "if": {
                  "path": "transfer_account_id",
                  "exists": true
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              }
            }
          }
        },
        {
          "name": "month_out",
          "value": {
            "sum": "outflow",
            "of": "ahead",
            "where": {
              "all": [
                {
                  "path": "days",
                  "gte": 0
                },
                {
                  "path": "days",
                  "lte": 29
                },
                {
                  "path": "transfer",
                  "equals": "no"
                },
                {
                  "path": "amount",
                  "lt": 0
                }
              ]
            }
          }
        },
        {
          "name": "month_in",
          "value": {
            "sum": "amount",
            "of": "ahead",
            "where": {
              "all": [
                {
                  "path": "days",
                  "gte": 0
                },
                {
                  "path": "days",
                  "lte": 29
                },
                {
                  "path": "transfer",
                  "equals": "no"
                },
                {
                  "path": "amount",
                  "gt": 0
                }
              ]
            }
          }
        },
        {
          "name": "repeats_soon",
          "value": {
            "count": "ahead",
            "where": {
              "all": [
                {
                  "path": "days",
                  "gte": 0
                },
                {
                  "path": "days",
                  "lte": 29
                },
                {
                  "path": "frequent",
                  "equals": "yes"
                }
              ]
            }
          }
        },
        {
          "name": "late",
          "value": {
            "each": "ahead",
            "where": {
              "path": "days",
              "lt": 0
            },
            "sort": "date",
            "fields": {
              "payee": "payee",
              "date": "date",
              "amount_text": "amount_text"
            }
          }
        },
        {
          "name": "late_count",
          "value": {
            "count": "late"
          }
        },
        {
          "name": "month_count",
          "value": {
            "count": "ahead",
            "where": {
              "all": [
                {
                  "path": "days",
                  "gte": 0
                },
                {
                  "path": "days",
                  "lte": 29
                }
              ]
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "out",
      "type": "stat",
      "label": "Scheduled out",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "scheduled",
        "map": {
          "value": {
            "path": "month_out",
            "round": 0
          },
          "delta": {
            "template": "next 30 days"
          },
          "unit": "settings.data.settings.currency_format.iso_code"
        }
      }
    },
    {
      "id": "in",
      "type": "stat",
      "label": "Scheduled in",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "scheduled",
        "map": {
          "value": {
            "path": "month_in",
            "round": 0
          },
          "delta": {
            "template": "next 30 days"
          },
          "unit": "settings.data.settings.currency_format.iso_code"
        }
      }
    },
    {
      "id": "horizons",
      "type": "tabs",
      "same_height": true,
      "tabs": [
        {
          "label": "Next 7 days",
          "cards": [
            {
              "id": "next-seven",
              "type": "agenda",
              "days": 7,
              "hide_all_day_label": true,
              "hide_empty_days": true,
              "max_events": 8,
              "items": [],
              "source": {
                "ref": "scheduled",
                "map": {
                  "items": {
                    "each": "ahead",
                    "sort": "date",
                    "limit": 60,
                    "where": {
                      "all": [
                        {
                          "path": "days",
                          "gte": 0
                        },
                        {
                          "path": "days",
                          "lte": 6
                        }
                      ]
                    },
                    "fields": {
                      "title": "payee",
                      "start": "date",
                      "detail": {
                        "template": "{amount_text} · {often} · {account}"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Next 30 days",
          "cards": [
            {
              "id": "next-thirty",
              "type": "agenda",
              "days": 30,
              "hide_all_day_label": true,
              "hide_empty_days": true,
              "max_events": 8,
              "items": [],
              "source": {
                "ref": "scheduled",
                "map": {
                  "items": {
                    "each": "ahead",
                    "sort": "date",
                    "limit": 60,
                    "where": {
                      "all": [
                        {
                          "path": "days",
                          "gte": 0
                        },
                        {
                          "path": "days",
                          "lte": 29
                        }
                      ]
                    },
                    "fields": {
                      "title": "payee",
                      "start": "date",
                      "detail": {
                        "template": "{amount_text} · {often} · {account}"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "repeats",
      "type": "text",
      "tone": "info",
      "body": "A schedule that repeats within the month shows only its next date, and counts once in the totals.",
      "source": {
        "ref": "scheduled",
        "when": {
          "path": "repeats_soon",
          "gt": 0
        }
      }
    },
    {
      "id": "more-scheduled",
      "type": "text",
      "tone": "info",
      "body": "Showing the first 60 scheduled in the next 30 days. See the rest in YNAB.",
      "source": {
        "ref": "scheduled",
        "when": {
          "path": "month_count",
          "gt": 60
        },
        "map": {
          "body": {
            "template": "Showing the first 60 of {month_count} scheduled in the next 30 days. See the rest in YNAB."
          }
        }
      }
    },
    {
      "id": "late",
      "type": "list",
      "label": "Past their date",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "scheduled",
        "when": {
          "path": "late_count",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "late",
            "limit": 60,
            "fields": {
              "text": "payee",
              "detail": {
                "template": "{amount_text} · {date|date}"
              }
            }
          }
        }
      }
    },
    {
      "id": "more-late",
      "type": "text",
      "tone": "info",
      "body": "Showing 60 schedules past their date. See the rest in YNAB.",
      "source": {
        "ref": "scheduled",
        "when": {
          "path": "late_count",
          "gt": 60
        },
        "map": {
          "body": {
            "template": "Showing 60 of {late_count} schedules past their date. See the rest in YNAB."
          }
        }
      }
    },
    {
      "id": "explore",
      "type": "list",
      "label": "Explore",
      "items": [
        {
          "text": "Budget",
          "url": "legato://page/ynab-budget"
        },
        {
          "text": "Accounts",
          "url": "legato://page/ynab-accounts"
        },
        {
          "text": "Open YNAB",
          "detail": "app.ynab.com",
          "url": "https://app.ynab.com"
        }
      ]
    }
  ]
}
