{
  "name": "Amazon Arbitrage Scanner",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 6 * * *"
            }
          ]
        }
      },
      "name": "Cron \u2014 Daily 06:00 UTC",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "id": "arb-cron-01"
    },
    {
      "parameters": {
        "functionCode": "// Scan retail categories for arbitrage\nconst categories = [\n  { store: 'walmart', category: 'toys', search: 'clearance toys' },\n  { store: 'target', category: 'baby', search: 'clearance baby products' },\n  { store: 'costco', category: 'electronics', search: 'clearance electronics' }\n];\n\nreturn categories.map(c => ({json: {...c, scan_id: `ARBO-${Date.now()}-${c.store}`}}));"
      },
      "name": "Define Scan Targets",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ],
      "id": "arb-targets"
    },
    {
      "parameters": {
        "url": "=https://api.scraperapi.com/?api_key={{$env['SCRAPERAPI_KEY']}}&url=https://www.{{$json.store}}.com/search?q={{encodeURIComponent($json.search)}}&render=true",
        "options": {
          "timeout": 30000
        }
      },
      "name": "Scrape Retail Store",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        680,
        300
      ],
      "id": "arb-scrape"
    },
    {
      "parameters": {
        "functionCode": "// Extract ASINs + prices from HTML (simplified \u2014 real impl via regex/cheerio)\nconst html = $input.item.json.body || '';\nconst source = $('Define Scan Targets').item.json;\n\n// Mock extraction \u2014 real: parse product cards\nconst products = [\n  { asin: 'B0MOCK001', title: 'Sample Toy Set', source_price: 9.99, category: source.category },\n  { asin: 'B0MOCK002', title: 'Baby Monitor Pro', source_price: 19.99, category: source.category }\n];\n\nreturn products.map(p => ({json: {...p, store: source.store, scan_id: source.scan_id}}));"
      },
      "name": "Parse Products",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        900,
        300
      ],
      "id": "arb-parse"
    },
    {
      "parameters": {
        "url": "=https://api.keepa.com/product?key={{$env['KEEPA_API_KEY']}}&domain=1&asin={{$json.asin}}&stats=90",
        "options": {
          "timeout": 20000
        }
      },
      "name": "Keepa \u2014 Amazon Price + BSR",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1120,
        300
      ],
      "id": "arb-keepa"
    },
    {
      "parameters": {
        "functionCode": "// ROI Calculator \u2014 wbudowany w n8n (duplikat engine/calculator.py)\nconst keepa = $input.item.json;\nconst product = $('Parse Products').item.json;\n\n// Wyci\u0105gnij Amazon price z Keepa\nconst products = keepa.products || [];\nconst amazonPrice = products[0]?.stats?.current?.[0] / 100 || 29.99; // Keepa prices in cents\nconst bsr = products[0]?.stats?.current?.[3] || 50000;\n\n// BSR \u2192 estimated monthly sales (simplified Jungle Scout formula)\nconst estSales = Math.max(1, Math.floor(3000000 / Math.pow(bsr, 0.8)));\n\n// ROI calc (mirrors engine/calculator.py)\nconst fbaFee = amazonPrice * 0.15;\nconst prepCost = 1.50;\nconst shipping = 0.50;\nconst totalCost = product.source_price + prepCost + shipping;\nconst netProfit = amazonPrice - fbaFee - totalCost;\nconst roi = totalCost > 0 ? netProfit / totalCost : 0;\nconst monthlyProfit = netProfit * estSales;\n\nconst viable = roi >= 0.20 && monthlyProfit >= 200;\n\nreturn [{\n  json: {\n    asin: product.asin,\n    title: product.title,\n    store: product.store,\n    source_price: product.source_price,\n    amazon_price: amazonPrice,\n    bsr,\n    est_sales_monthly: estSales,\n    fba_fee: Math.round(fbaFee * 100) / 100,\n    net_profit: Math.round(netProfit * 100) / 100,\n    roi: Math.round(roi * 10000) / 100,  // as %\n    monthly_profit: Math.round(monthlyProfit * 100) / 100,\n    viable,\n    scan_id: product.scan_id\n  }\n}];"
      },
      "name": "ROI Calculator",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1340,
        300
      ],
      "id": "arb-roi"
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.viable}}",
              "value2": true
            }
          ]
        }
      },
      "name": "ROI Viable?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1560,
        300
      ],
      "id": "arb-if"
    },
    {
      "parameters": {
        "url": "={{$env['GOOGLE_SHEETS_WEBHOOK']}}",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "asin",
              "value": "={{$json.asin}}"
            },
            {
              "name": "title",
              "value": "={{$json.title}}"
            },
            {
              "name": "roi",
              "value": "={{$json.roi}}"
            },
            {
              "name": "monthly_profit",
              "value": "={{$json.monthly_profit}}"
            },
            {
              "name": "source_price",
              "value": "={{$json.source_price}}"
            },
            {
              "name": "amazon_price",
              "value": "={{$json.amazon_price}}"
            }
          ]
        }
      },
      "name": "Save to Sheets",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1780,
        220
      ],
      "id": "arb-sheets"
    },
    {
      "parameters": {
        "url": "={{$env['SLACK_WEBHOOK_URL']}}",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "text",
              "value": "=\ud83d\udcb0 *Arbitrage Alert!* ASIN: `{{$json.asin}}`\n\ud83d\udce6 {{$json.title}}\n\ud83c\udfea Buy: ${{$json.source_price}} @ {{$json.store}}\n\ud83d\uded2 Sell: ${{$json.amazon_price}} on Amazon\n\ud83d\udcc8 ROI: {{$json.roi}}% | Monthly profit: ${{$json.monthly_profit}}"
            }
          ]
        }
      },
      "name": "Slack Alert",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1780,
        380
      ],
      "id": "arb-slack"
    }
  ],
  "connections": {
    "Cron \u2014 Daily 06:00 UTC": {
      "main": [
        [
          {
            "node": "Define Scan Targets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Define Scan Targets": {
      "main": [
        [
          {
            "node": "Scrape Retail Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Scrape Retail Store": {
      "main": [
        [
          {
            "node": "Parse Products",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Products": {
      "main": [
        [
          {
            "node": "Keepa \u2014 Amazon Price + BSR",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Keepa \u2014 Amazon Price + BSR": {
      "main": [
        [
          {
            "node": "ROI Calculator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ROI Calculator": {
      "main": [
        [
          {
            "node": "ROI Viable?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ROI Viable?": {
      "main": [
        [
          {
            "node": "Save to Sheets",
            "type": "main",
            "index": 0
          },
          {
            "node": "Slack Alert",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "UTC"
  },
  "tags": [
    "arbitrage",
    "amazon",
    "roi",
    "scanner"
  ],
  "versionId": "1.0.0"
}