{
  "name": "LGM 05 — Scheduled Reporting (SIMULATED)",
  "nodes": [
    {
      "parameters": {},
      "id": "f05-start",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Fictional scheduling trigger payload. Replace with Schedule Trigger\n// (cron) when going live.\nconst fixture = {\n  schedule_id: 'SCH-' + Math.floor(Math.random() * 9000 + 1000),\n  period: 'week',\n  focus_area: 'lead response time',\n  triggered_at: new Date().toISOString(),\n  // Fictional metric set — replace with Twenty CRM aggregates in production.\n  metrics: {\n    new_enquiries: 12,\n    median_first_response_min: 11,\n    bookings_from_ai: 4,\n    reviews_collected: 7,\n    quotes_overdue: 2\n  },\n  anomaly_hint: false,\n  recipient: 'owner@example.invalid'\n};\nreturn [{ json: fixture }];"
      },
      "id": "f05-fixture",
      "name": "Build Fixture",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "leftValue": "={{ $json.anomaly_hint }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ]
        }
      },
      "id": "f05-anom",
      "name": "Anomaly?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.period }}",
                    "rightValue": "month",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "outputKey": "monthly",
              "renameOutput": true
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.period }}",
                    "rightValue": "skip",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "outputKey": "skip",
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "id": "f05-switch",
      "name": "Choose Cadence",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        900,
        460
      ]
    },
    {
      "parameters": {
        "jsCode": "// Anomaly path — internal alert + investigation task.\nconst items = $input.all();\nconst $json = items[0].json;\nconst simulatedOutputs = [\n  { type: 'heading', label: '1. Internal alert (SIMULATED Slack/Teams)' },\n  { type: 'message', channel: 'internal', label: 'Slack/Teams',\n    body: `Anomaly detected: lead-response median jumped from ${$json.metrics.median_first_response_min - 36}m to ${$json.metrics.median_first_response_min}m on Tuesday. Possible cause: roster gap or workflow paused. Suggest manual review of n8n execution log.` },\n  { type: 'heading', label: '2. Investigation task (SIMULATED Todoist)' },\n  { type: 'task', label: 'Todoist', title: 'Investigate lead-response spike',\n    owner: 'Operations', dueIn: 'Today', notes: `Ref: ${$json.schedule_id}` }\n];\nreturn [{ json: { ...$json, branch: 'anomaly', simulatedOutputs } }];"
      },
      "id": "f05-anom-out",
      "name": "Build Anomaly Output",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "// Weekly summary report.\nconst items = $input.all();\nconst $json = items[0].json;\nconst m = $json.metrics || {};\nconst simulatedOutputs = [\n  { type: 'heading', label: '1. Email report (SIMULATED Listmonk)' },\n  { type: 'message', channel: 'email', label: 'Listmonk',\n    subject: `${$json.period[0].toUpperCase()}${$json.period.slice(1)} growth snapshot — focus: ${$json.focus_area}`,\n    body: `This ${$json.period}'s snapshot (illustrative numbers):\n\n  • New enquiries: ${m.new_enquiries}\n  • Median first-response time: ${m.median_first_response_min} min\n  • Bookings from AI-led conversations: ${m.bookings_from_ai}\n  • Reviews collected (same neutral link): ${m.reviews_collected}\n  • Outstanding quotes older than 5 days: ${m.quotes_overdue}\n\nFull breakdown attached.\n\n— LetsGrowMate automated report` },\n  { type: 'note', label: 'Production note',\n    text: 'Numbers shown are illustrative. Production numbers come from Twenty CRM + connected integrations.' }\n];\nreturn [{ json: { ...$json, branch: 'weekly', simulatedOutputs } }];"
      },
      "id": "f05-weekly",
      "name": "Build Weekly",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "// Monthly executive report.\nconst items = $input.all();\nconst $json = items[0].json;\nconst simulatedOutputs = [\n  { type: 'heading', label: '1. Email report (SIMULATED Listmonk)' },\n  { type: 'message', channel: 'email', label: 'Listmonk', subject: 'Monthly executive report',\n    body: `This month at a glance (illustrative):\n\n  • Pipeline created vs closed (configurable)\n  • Cost per lead by channel (configurable)\n  • Tracked search rankings vs last month\n  • Sampled AI-answer observations (configurable)\n\nFull report PDF attached. Want a walk-through? Reply and we'll book 20 minutes.\n\n— LetsGrowMate automated report` }\n];\nreturn [{ json: { ...$json, branch: 'monthly', simulatedOutputs } }];"
      },
      "id": "f05-monthly",
      "name": "Build Monthly",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        540
      ]
    },
    {
      "parameters": {
        "jsCode": "// Skip path — public-holiday pause or cadence override.\nconst items = $input.all();\nconst $json = items[0].json;\nconst simulatedOutputs = [\n  { type: 'heading', label: '1. Internal note (SIMULATED Slack/Teams)' },\n  { type: 'message', channel: 'internal', label: 'Slack/Teams',\n    body: `No report due this period — cadence override applied (e.g. public holidays or customer-requested pause). Next send in 7 days.` }\n];\nreturn [{ json: { ...$json, branch: 'skip', simulatedOutputs } }];"
      },
      "id": "f05-skip",
      "name": "Build Skip Output",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        680
      ]
    },
    {
      "parameters": {},
      "id": "f05-sink",
      "name": "Sink",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1380,
        480
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Build Fixture",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Fixture": {
      "main": [
        [
          {
            "node": "Anomaly?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Anomaly?": {
      "main": [
        [
          {
            "node": "Build Anomaly Output",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Choose Cadence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Choose Cadence": {
      "main": [
        [
          {
            "node": "Build Weekly",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Monthly",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Skip Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Anomaly Output": {
      "main": [
        [
          {
            "node": "Sink",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Weekly": {
      "main": [
        [
          {
            "node": "Sink",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Monthly": {
      "main": [
        [
          {
            "node": "Sink",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Skip Output": {
      "main": [
        [
          {
            "node": "Sink",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner"
  },
  "versionId": "lgm-05-scheduled-reporting",
  "id": "lgm-05-scheduled-reporting",
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "SIMULATED scheduled reporting. Replace manual trigger with Schedule Trigger; replace fixture metrics with Twenty CRM aggregates."
  },
  "tags": [
    {
      "name": "lgm-05-scheduled-reporting"
    }
  ]
}
