{
  "name": "SmartMart Daily Operations Report",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 6 * * *"
            }
          ]
        }
      },
      "id": "f2c8d0e1-2222-4c3b-ad4e-111111111111",
      "name": "Daily 06:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "url": "={{ $env.SMARTMART_BASE_URL }}/api/v1/ai-operations/snapshot",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SMARTMART_N8N_TOKEN }}"
            },
            {
              "name": "Cookie",
              "value": "tunnel_phishing_protection=$env.TUNNEL_COOKIE"
            }
          ]
        },
        "sendQuery": false,
        "sendBody": false,
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "f2c8d0e1-2222-4c3b-ad4e-111111111112",
      "name": "Collect snapshot",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "url": "={{ $env.SMARTMART_BASE_URL }}/api/v1/ai-operations/kpis",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SMARTMART_N8N_TOKEN }}"
            },
            {
              "name": "Cookie",
              "value": "tunnel_phishing_protection=$env.TUNNEL_COOKIE"
            }
          ]
        },
        "sendQuery": false,
        "sendBody": false,
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "f2c8d0e1-2222-4c3b-ad4e-111111111113",
      "name": "Collect KPIs",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 220]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const snapBody = $('Collect snapshot').first().json;\nconst snap = snapBody.data ?? snapBody;\nconst kpiBody = $('Collect KPIs').first().json;\nconst kpis = kpiBody.data ?? kpiBody;\nconst risk = snap.risk || {};\nconst incs = Array.isArray(snap.active_incidents) ? snap.active_incidents : [];\nconst lines = [];\nlines.push(':bar_chart: SmartMart Daily AI Operations Report - ' + new Date().toISOString());\nlines.push('');\nlines.push('Risk score: ' + (risk.score ?? '?') + '/100 (' + (risk.severity || 'NORMAL') + ')');\nlines.push('Operational health: ' + (kpis.operational_health_score ?? '?'));\nlines.push('Order volume change: ' + (snap.orders ? snap.orders.order_volume_change_percent : '?') + '%');\nlines.push('Backlog: ' + (snap.orders ? snap.orders.backlog : '?'));\nlines.push('Avg fulfillment: ' + (snap.orders ? snap.orders.average_fulfillment_minutes : '?') + ' min');\nlines.push('SLA breaches: ' + (snap.orders ? snap.orders.sla_breaches : '?'));\nlines.push('Low stock items: ' + (snap.inventory ? snap.inventory.low_stock_items : '?'));\nlines.push('Out of stock items: ' + (snap.inventory ? snap.inventory.out_of_stock_items : '?'));\nlines.push('Staff capacity gap: ' + (snap.workforce ? snap.workforce.capacity_gap_percent : '?') + '%');\nlines.push('Open tickets: ' + (snap.support ? snap.support.open_tickets : '?'));\nlines.push('Complaint change: ' + (snap.support ? snap.support.complaint_change_percent : '?') + '%');\nlines.push('');\nif (incs.length > 0) {\n  lines.push('Active incidents (' + incs.length + '):');\n  for (const inc of incs) {\n    lines.push('- ' + inc.incident_id + ' [' + inc.severity + ' ' + inc.risk_score + '] ' + (inc.summary || ''));\n  }\n} else {\n  lines.push('No active incidents.');\n}\nreturn [{\n  json: {\n    report: lines.join('\\n'),\n    date: new Date().toISOString().slice(0, 10),\n    riskScore: risk.score ?? 0,\n    activeIncidents: incs.length\n  }\n}];"
      },
      "id": "f2c8d0e1-2222-4c3b-ad4e-111111111114",
      "name": "Build report",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "conditions": {
          "string": {
            "string": [
              {
                "value1": "={{ $env.SLACK_WEBHOOK_URL || '' }}",
                "operation": "notEmpty"
              }
            ]
          }
        },
        "options": {}
      },
      "id": "f2c8d0e1-2222-4c3b-ad4e-111111111115",
      "name": "Slack configured?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.1,
      "position": [660, 0]
    },
    {
      "parameters": {
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: $('Build report').first().json.report }) }}",
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "f2c8d0e1-2222-4c3b-ad4e-111111111116",
      "name": "Send Slack report",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, -120]
    },
    {
      "parameters": {},
      "id": "f2c8d0e1-2222-4c3b-ad4e-111111111117",
      "name": "Report generated (no channel)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 0.1,
      "position": [880, 200]
    }
  ],
  "connections": {
    "Daily 06:00": {
      "main": [
        [{ "node": "Collect snapshot", "type": "main", "index": 0 }],
        [{ "node": "Collect KPIs", "type": "main", "index": 0 }]
      ]
    },
    "Collect snapshot": {
      "main": [[{ "node": "Build report", "type": "main", "index": 0 }]]
    },
    "Collect KPIs": {
      "main": []
    },
    "Build report": {
      "main": [[{ "node": "Slack configured?", "type": "main", "index": 0 }]]
    },
    "Slack configured?": {
      "main": [
        [{ "node": "Send Slack report", "type": "main", "index": 0 }],
        [{ "node": "Report generated (no channel)", "type": "main", "index": 0 }]
      ]
    }
  },
  "settings": {
    "errorWorkflow": "SmartMart Error Handler",
    "timezone": "Africa/Lagos",
    "executionOrder": "v1"
  }
}