{
  "name": "CSV Data Analyst",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "analyze-csv",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "cda0001-0001-4000-8000-000000000001",
      "name": "Receive CSV Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "url": "={{ $json.body.csvUrl }}",
        "options": {
          "timeout": 30000
        }
      },
      "id": "cda0001-0002-4000-8000-000000000002",
      "name": "Fetch CSV File",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [460, 300]
    },
    {
      "parameters": {
        "jsCode": "const csvText = $input.item.json.data || '';\nconst lines = csvText.trim().split('\\n');\nconst headers = lines[0].split(',').map(h => h.trim().replace(/\"/g, ''));\nconst rows = lines.slice(1).map(line => {\n  const values = line.split(',').map(v => v.trim().replace(/\"/g, ''));\n  return headers.reduce((obj, h, i) => ({ ...obj, [h]: values[i] || '' }), {});\n});\nconst numericCols = headers.filter(h => rows.some(r => !isNaN(parseFloat(r[h]))));\nconst stats = numericCols.reduce((acc, col) => {\n  const vals = rows.map(r => parseFloat(r[col])).filter(v => !isNaN(v));\n  const sum = vals.reduce((a, b) => a + b, 0);\n  acc[col] = { count: vals.length, sum, mean: sum / vals.length, min: Math.min(...vals), max: Math.max(...vals) };\n  return acc;\n}, {});\nreturn [{ json: { headers, rowCount: rows.length, numericColumns: numericCols, stats, sampleRows: rows.slice(0, 5) } }];"
      },
      "id": "cda0001-0003-4000-8000-000000000003",
      "name": "Parse and Profile CSV",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "resource": "text",
        "operation": "message",
        "modelId": {
          "__rl": true,
          "value": "gpt-4o",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "content": "You are a data analyst. Analyze this CSV dataset profile and answer the user's question. Provide actionable insights.\n\nDataset Overview:\n- Columns: {{ $json.headers.join(', ') }}\n- Total rows: {{ $json.rowCount }}\n- Numeric columns: {{ $json.numericColumns.join(', ') }}\n- Statistics: {{ JSON.stringify($json.stats, null, 2) }}\n- Sample rows: {{ JSON.stringify($json.sampleRows, null, 2) }}\n\nUser Question: {{ $('Receive CSV Webhook').item.json.body.question }}\n\nProvide a clear, structured analysis with key findings and recommendations."
            }
          ]
        },
        "options": {
          "temperature": 0.2
        }
      },
      "id": "cda0001-0004-4000-8000-000000000004",
      "name": "Analyze with AI",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1,
      "position": [900, 300]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "analysis",
              "value": "={{ $json.message.content }}",
              "type": "string"
            },
            {
              "name": "rowCount",
              "value": "={{ $('Parse and Profile CSV').item.json.rowCount }}",
              "type": "number"
            },
            {
              "name": "columns",
              "value": "={{ $('Parse and Profile CSV').item.json.headers }}",
              "type": "array"
            }
          ]
        }
      },
      "id": "cda0001-0005-4000-8000-000000000005",
      "name": "Return Analysis",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [1120, 300]
    }
  ],
  "connections": {
    "Receive CSV Webhook": {
      "main": [[{ "node": "Fetch CSV File", "type": "main", "index": 0 }]]
    },
    "Fetch CSV File": {
      "main": [[{ "node": "Parse and Profile CSV", "type": "main", "index": 0 }]]
    },
    "Parse and Profile CSV": {
      "main": [[{ "node": "Analyze with AI", "type": "main", "index": 0 }]]
    },
    "Analyze with AI": {
      "main": [[{ "node": "Return Analysis", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "meta": {},
  "pinData": {}
}
