{
  "name": "Automated Weekly Report — Metrics, Deltas & AI Summary",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [1],
              "triggerAtHour": 8,
              "triggerAtMinute": 0
            }
          ]
        }
      },
      "id": "f1a2b3c4-1111-4f2a-8b3c-0d1e2f3a4b5c",
      "name": "Schedule: Monday 8am",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [-240, 300]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Weekly Metrics",
          "mode": "name"
        },
        "options": {}
      },
      "id": "a2b3c4d5-2222-4a3b-9c4d-1e2f3a4b5c6d",
      "name": "Google Sheets: Read Metrics",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [0, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL",
          "name": "YOUR_GOOGLE_SHEETS_CREDENTIAL"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Expects rows sorted by week, one row per week, with columns:\n// Week, Revenue, New Customers, Churned, Website Visits, Signups\nconst rows = $input.all().map((i) => i.json);\nif (rows.length < 2) {\n  throw new Error('Need at least 2 rows of weekly metrics to compute deltas');\n}\nconst current = rows[rows.length - 1];\nconst previous = rows[rows.length - 2];\n\nconst metrics = ['Revenue', 'New Customers', 'Churned', 'Website Visits', 'Signups'];\nconst deltas = {};\nfor (const m of metrics) {\n  const cur = Number(current[m]) || 0;\n  const prev = Number(previous[m]) || 0;\n  deltas[m] = {\n    current: cur,\n    previous: prev,\n    change: cur - prev,\n    change_pct: prev !== 0 ? Number((((cur - prev) / prev) * 100).toFixed(1)) : null,\n  };\n}\n\nreturn [{\n  json: {\n    week: current.Week,\n    previous_week: previous.Week,\n    deltas,\n  },\n}];"
      },
      "id": "b3c4d5e6-3333-4b4c-8d5e-2f3a4b5c6d7e",
      "name": "Code: Compute Deltas",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "gpt-4o-mini"
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a business analyst writing a Monday-morning metrics summary for a small SaaS team.\n\nRules:\n- Start with a one-sentence headline verdict (good week / mixed week / rough week and why).\n- Then 3-5 bullet points: the most important movements with numbers and % change.\n- Call out anything that moved more than 15% in either direction.\n- End with one suggested focus for the coming week based on the data.\n- Plain text with simple dashes for bullets. No markdown headers, no tables, under 200 words. Do not invent numbers that are not in the data."
            },
            {
              "role": "user",
              "content": "=Write the weekly summary for week {{ $json.week }} (vs {{ $json.previous_week }}) from this data:\n{{ JSON.stringify($json.deltas, null, 2) }}"
            }
          ]
        },
        "options": {
          "temperature": 0.3
        }
      },
      "id": "c4d5e6f7-4444-4c5d-9e6f-3a4b5c6d7e8f",
      "name": "OpenAI: Analyst Summary",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [480, 300],
      "credentials": {
        "openAiApi": {
          "id": "YOUR_OPENAI_CREDENTIAL",
          "name": "YOUR_OPENAI_CREDENTIAL"
        }
      }
    },
    {
      "parameters": {
        "sendTo": "team@yourcompany.com",
        "subject": "=Weekly metrics report — week of {{ $('Code: Compute Deltas').item.json.week }}",
        "emailType": "text",
        "message": "=Good morning,\n\nHere is the automated weekly report:\n\n{{ $json.message?.content ?? $json.text }}\n\nFull data: https://docs.google.com/spreadsheets/d/YOUR_SPREADSHEET_ID\n\n— AutoFlow weekly report bot",
        "options": {}
      },
      "id": "d5e6f7a8-5555-4d6e-8f7a-4b5c6d7e8f9a",
      "name": "Gmail: Send Report",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [720, 180],
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CREDENTIAL",
          "name": "YOUR_GMAIL_CREDENTIAL"
        }
      }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "#general",
          "mode": "name"
        },
        "text": "=:bar_chart: *Weekly report — week of {{ $('Code: Compute Deltas').item.json.week }}*\n\n{{ $json.message?.content ?? $json.text }}",
        "otherOptions": {}
      },
      "id": "e6f7a8b9-6666-4e7f-9a8b-5c6d7e8f9a0b",
      "name": "Slack: Post Report",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [720, 420],
      "credentials": {
        "slackApi": {
          "id": "YOUR_SLACK_CREDENTIAL",
          "name": "YOUR_SLACK_CREDENTIAL"
        }
      }
    }
  ],
  "connections": {
    "Schedule: Monday 8am": {
      "main": [
        [
          {
            "node": "Google Sheets: Read Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets: Read Metrics": {
      "main": [
        [
          {
            "node": "Code: Compute Deltas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Compute Deltas": {
      "main": [
        [
          {
            "node": "OpenAI: Analyst Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI: Analyst Summary": {
      "main": [
        [
          {
            "node": "Gmail: Send Report",
            "type": "main",
            "index": 0
          },
          {
            "node": "Slack: Post Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
