{
  "$schema": "./.schemas/page.schema.json",
  "title": "Lunch Money",
  "icon": "dollarsign.circle",
  "sources": {
    "money": {
      "url": "https://api.lunchmoney.dev",
      "secret": "lunch-money",
      "every": "1h",
      "headers": {
        "Accept": "application/json"
      },
      "requests": {
        "banks": "/v2/plaid_accounts",
        "me": "/v2/me"
      },
      "derive": [
        {
          "name": "linked",
          "value": {
            "count": "banks.plaid_accounts",
            "where": {
              "path": "status",
              "not": "inactive"
            }
          }
        },
        {
          "name": "stale",
          "value": {
            "count": "banks.plaid_accounts",
            "where": {
              "path": "status",
              "not_in": [
                "active",
                "inactive",
                "syncing"
              ]
            }
          }
        },
        {
          "name": "cash",
          "value": {
            "sum": "to_base",
            "of": "banks.plaid_accounts",
            "where": {
              "path": "type",
              "equals": "cash"
            }
          }
        },
        {
          "name": "investments",
          "value": {
            "sum": "to_base",
            "of": "banks.plaid_accounts",
            "where": {
              "path": "type",
              "equals": "investment"
            }
          }
        },
        {
          "name": "credit",
          "value": {
            "sum": "to_base",
            "of": "banks.plaid_accounts",
            "where": {
              "path": "type",
              "equals": "credit"
            }
          }
        },
        {
          "name": "loans",
          "value": {
            "sum": "to_base",
            "of": "banks.plaid_accounts",
            "where": {
              "path": "type",
              "equals": "loan"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "sync",
      "type": "status",
      "label": "Bank connections",
      "state": "unknown",
      "source": {
        "ref": "money",
        "map": {
          "state": {
            "if": {
              "path": "linked",
              "equals": 0
            },
            "then": {
              "template": "unknown"
            },
            "else": {
              "path": "stale",
              "values": {
                "0": "ok"
              },
              "default": "warning"
            }
          },
          "detail": {
            "if": {
              "path": "linked",
              "equals": 0
            },
            "then": {
              "template": "No linked bank accounts"
            },
            "else": {
              "if": {
                "path": "stale",
                "equals": 0
              },
              "then": {
                "template": "All {linked} linked accounts syncing"
              },
              "else": {
                "template": "{stale} of {linked} accounts need attention"
              }
            }
          }
        }
      },
      "actions": [
        {
          "label": "Sync banks",
          "icon": "arrow.triangle.2.circlepath",
          "method": "POST",
          "path": "/v2/plaid_accounts/fetch",
          "error": "errors[0].errMsg"
        }
      ]
    },
    {
      "id": "balances",
      "type": "bars",
      "label": "Balances by type",
      "unit": "USD",
      "items": [
        {
          "label": "Cash",
          "value": 0
        },
        {
          "label": "Investments",
          "value": 0
        },
        {
          "label": "Credit cards",
          "value": 0
        },
        {
          "label": "Loans",
          "value": 0
        }
      ],
      "source": {
        "ref": "money",
        "map": {
          "items[0].value": {
            "path": "cash",
            "round": 0
          },
          "items[1].value": {
            "path": "investments",
            "round": 0
          },
          "items[2].value": {
            "path": "credit",
            "round": 0
          },
          "items[3].value": {
            "path": "loans",
            "round": 0
          },
          "unit": "me.primary_currency"
        }
      }
    },
    {
      "id": "cash",
      "type": "stat",
      "label": "Cash",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "money",
        "map": {
          "value": {
            "path": "cash",
            "round": 0
          },
          "unit": "me.primary_currency"
        }
      }
    },
    {
      "id": "card-debt",
      "type": "stat",
      "label": "Credit cards",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "money",
        "map": {
          "value": {
            "path": "credit",
            "round": 0
          },
          "unit": "me.primary_currency"
        }
      }
    },
    {
      "id": "open",
      "type": "link",
      "label": "Open Lunch Money",
      "url": "https://my.lunchmoney.app"
    }
  ]
}
