{
  "name": "Google Sheets Scheduled Weekly Report",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [{ "field": "weeks", "weeksInterval": 1, "triggerAtDay": 1, "triggerAtHour": 8 }]
        }
      },
      "id": "ssrp0001-0001-4000-8000-000000000001",
      "name": "Weekly Monday Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": { "__rl": true, "mode": "id", "value": "={{ $env.METRICS_SHEET_ID }}" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Weekly Metrics" },
        "options": { "headerRow": 1 }
      },
      "id": "ssrp0001-0002-4000-8000-000000000002",
      "name": "Read Metrics Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [460, 300]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "fieldsToAggregate": {
          "fieldToAggregate": [
            { "fieldToAggregate": "revenue", "renameField": true, "outputFieldName": "total_revenue" },
            { "fieldToAggregate": "signups", "renameField": true, "outputFieldName": "total_signups" },
            { "fieldToAggregate": "churn", "renameField": true, "outputFieldName": "total_churn" }
          ]
        },
        "options": {}
      },
      "id": "ssrp0001-0003-4000-8000-000000000003",
      "name": "Aggregate Totals",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [680, 300]
    },
    {
      "parameters": {
        "jsCode": "const d = items[0].json;\nconst week = new Date().toISOString().slice(0,10);\nconst revenue = (d.total_revenue || []).reduce((a,b) => a + Number(b), 0);\nconst signups = (d.total_signups || []).reduce((a,b) => a + Number(b), 0);\nconst churn = (d.total_churn || []).reduce((a,b) => a + Number(b), 0);\nreturn [{ json: { week, revenue, signups, churn, net_growth: signups - churn, report_html: `<h2>Weekly Report – ${week}</h2><ul><li>Revenue: $${revenue.toLocaleString()}</li><li>New Signups: ${signups}</li><li>Churn: ${churn}</li><li>Net Growth: ${signups - churn}</li></ul>` } }];"
      },
      "id": "ssrp0001-0004-4000-8000-000000000004",
      "name": "Build Report",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [900, 300]
    },
    {
      "parameters": {
        "sendTo": "={{ $env.REPORT_EMAIL }}",
        "subject": "Weekly Metrics Report – {{ $json.week }}",
        "emailType": "html",
        "message": "={{ $json.report_html }}",
        "options": {}
      },
      "id": "ssrp0001-0005-4000-8000-000000000005",
      "name": "Email Weekly Report",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [1120, 300]
    }
  ],
  "connections": {
    "Weekly Monday Trigger": {
      "main": [[{ "node": "Read Metrics Sheet", "type": "main", "index": 0 }]]
    },
    "Read Metrics Sheet": {
      "main": [[{ "node": "Aggregate Totals", "type": "main", "index": 0 }]]
    },
    "Aggregate Totals": {
      "main": [[{ "node": "Build Report", "type": "main", "index": 0 }]]
    },
    "Build Report": {
      "main": [[{ "node": "Email Weekly Report", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "meta": {},
  "pinData": {}
}
