{
  "$schema": "./.schemas/page.schema.json",
  "title": "Accounts",
  "icon": "building.columns",
  "sources": {
    "plan": {
      "url": "https://api.ynab.com",
      "secret": "ynab",
      "every": "1h",
      "requests": {
        "accounts": "/v1/plans/last-used/accounts",
        "settings": "/v1/plans/last-used/settings"
      },
      "derive": [
        {
          "name": "linked",
          "value": {
            "count": "accounts.data.accounts",
            "where": {
              "all": [
                {
                  "path": "direct_import_linked",
                  "equals": true
                },
                {
                  "path": "closed",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                }
              ]
            }
          }
        },
        {
          "name": "broken",
          "value": {
            "count": "accounts.data.accounts",
            "where": {
              "all": [
                {
                  "path": "direct_import_linked",
                  "equals": true
                },
                {
                  "path": "closed",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "direct_import_in_error",
                  "equals": true
                }
              ]
            }
          }
        },
        {
          "name": "cash",
          "value": {
            "sum": "balance_currency",
            "of": "accounts.data.accounts",
            "where": {
              "all": [
                {
                  "path": "closed",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "type",
                  "in": [
                    "checking",
                    "savings",
                    "cash"
                  ]
                },
                {
                  "path": "on_budget",
                  "equals": true
                }
              ]
            }
          }
        },
        {
          "name": "cards",
          "value": {
            "sum": "balance_currency",
            "of": "accounts.data.accounts",
            "where": {
              "all": [
                {
                  "path": "closed",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                },
                {
                  "path": "type",
                  "in": [
                    "creditCard",
                    "lineOfCredit"
                  ]
                }
              ]
            }
          }
        },
        {
          "name": "net",
          "value": {
            "sum": "balance_currency",
            "of": "accounts.data.accounts",
            "where": {
              "path": "deleted",
              "not": true
            }
          }
        },
        {
          "name": "open_count",
          "value": {
            "count": "accounts.data.accounts",
            "where": {
              "all": [
                {
                  "path": "closed",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                }
              ]
            }
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "accounts.data.accounts",
            "where": {
              "all": [
                {
                  "path": "closed",
                  "not": true
                },
                {
                  "path": "deleted",
                  "not": true
                }
              ]
            },
            "fields": {
              "id": "id",
              "name": "name",
              "balance": "balance_currency",
              "balance_text": "balance_formatted",
              "type": "type",
              "section": {
                "if": {
                  "path": "on_budget",
                  "equals": true
                },
                "then": {
                  "template": "On budget"
                },
                "else": {
                  "template": "Tracking"
                }
              },
              "section_rank": {
                "if": {
                  "path": "on_budget",
                  "equals": true
                },
                "then": {
                  "template": "0"
                },
                "else": {
                  "template": "1"
                }
              },
              "attention": {
                "if": {
                  "all": [
                    {
                      "path": "direct_import_linked",
                      "equals": true
                    },
                    {
                      "path": "direct_import_in_error",
                      "equals": true
                    }
                  ]
                },
                "then": {
                  "template": "0"
                },
                "else": {
                  "template": "1"
                }
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "connections",
      "type": "status",
      "placement": "page_info",
      "label": "Bank connections",
      "width": "full",
      "state": "unknown",
      "url": "https://app.ynab.com",
      "source": {
        "ref": "plan",
        "map": {
          "state": {
            "if": {
              "path": "linked",
              "equals": 0
            },
            "then": {
              "template": "off"
            },
            "else": {
              "path": "broken",
              "values": {
                "0": "ok"
              },
              "default": "warning"
            }
          },
          "detail": {
            "if": {
              "path": "linked",
              "equals": 0
            },
            "then": {
              "template": "No accounts linked to a bank"
            },
            "else": {
              "if": {
                "path": "broken",
                "equals": 0
              },
              "then": {
                "path": "linked",
                "values": {
                  "1": "The linked account is importing"
                },
                "default": {
                  "template": "All {linked} linked accounts importing"
                }
              },
              "else": {
                "template": "{broken} of {linked} linked accounts need reconnecting"
              }
            }
          }
        }
      }
    },
    {
      "id": "cash",
      "type": "stat",
      "label": "Cash",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "plan",
        "map": {
          "value": {
            "path": "cash",
            "round": 0
          },
          "unit": "settings.data.settings.currency_format.iso_code"
        }
      }
    },
    {
      "id": "cards",
      "type": "stat",
      "label": "Credit cards",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "plan",
        "map": {
          "value": {
            "path": "cards",
            "round": 0
          },
          "unit": "settings.data.settings.currency_format.iso_code"
        }
      }
    },
    {
      "id": "net-worth",
      "type": "sparkline",
      "label": "Net worth",
      "unit": "USD",
      "source": {
        "ref": "plan",
        "map": {
          "points": {
            "history": "net",
            "every": "1d",
            "keep": 60
          },
          "unit": "settings.data.settings.currency_format.iso_code"
        }
      }
    },
    {
      "id": "accounts",
      "type": "list",
      "label": "Accounts",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "plan",
        "empty": {
          "when": {
            "path": "rows[0]",
            "exists": false
          },
          "text": "No open accounts"
        },
        "map": {
          "items": {
            "each": "rows",
            "sort": {
              "by": [
                "section_rank",
                "attention",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "key": "id",
              "section": "section",
              "text": {
                "if": {
                  "path": "attention",
                  "equals": "0"
                },
                "then": {
                  "template": "{name} · Reconnect in YNAB"
                },
                "else": {
                  "template": "{name}"
                }
              },
              "detail": "balance_text",
              "icon": {
                "if": {
                  "path": "attention",
                  "equals": "0"
                },
                "then": {
                  "template": "exclamationmark.triangle"
                },
                "else": {
                  "path": "type",
                  "values": {
                    "checking": "building.columns",
                    "savings": "banknote",
                    "cash": "dollarsign",
                    "creditCard": "creditcard",
                    "lineOfCredit": "creditcard",
                    "mortgage": "house",
                    "autoLoan": "car",
                    "studentLoan": "graduationcap",
                    "personalLoan": "person",
                    "medicalDebt": "cross.case",
                    "otherAsset": "chart.line.uptrend.xyaxis"
                  },
                  "default": "tag"
                }
              },
              "color": {
                "path": "attention",
                "values": {
                  "0": "orange"
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "more-accounts",
      "type": "text",
      "tone": "info",
      "body": "Showing 60 open accounts. See the rest in YNAB.",
      "source": {
        "ref": "plan",
        "when": {
          "path": "open_count",
          "gt": 60
        },
        "map": {
          "body": {
            "template": "Showing 60 of {open_count} open accounts. See the rest in YNAB."
          }
        }
      }
    },
    {
      "id": "explore",
      "type": "list",
      "label": "Explore",
      "items": [
        {
          "text": "Budget",
          "url": "legato://page/ynab-budget"
        },
        {
          "text": "Upcoming",
          "url": "legato://page/ynab-upcoming"
        },
        {
          "text": "Open YNAB",
          "detail": "app.ynab.com",
          "url": "https://app.ynab.com"
        }
      ]
    }
  ]
}
