{
  "name": "Homes.com Deal Enrichment",
  "version": 1,
  "nodes": [
    {
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "name": "Read Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 3,
      "position": [
        250,
        0
      ],
      "parameters": {
        "operation": "read",
        "sheetName": "",
        "rangeDefinition": "Data",
        "valueRenderMode": "UNFORMATTED_VALUE",
        "returnAll": true
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        500,
        0
      ],
      "parameters": {
        "batchSize": 1
      }
    },
    {
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        750,
        0
      ],
      "parameters": {
        "authentication": "none",
        "url": "={{$json[\"Homes_URL\"]}}",
        "responseFormat": "string"
      }
    },
    {
      "name": "Extract Deals",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1000,
        0
      ],
      "parameters": {
        "functionCode": "const html = items[0].json;\nconst text = typeof html === \"string\" ? html : JSON.stringify(html);\nfunction extractNumberAfter(label) {\n  const idx = text.indexOf(label);\n  if (idx === -1) return 0;\n  const slice = text.slice(idx, idx + 400);\n  const match = slice.match(/(\\d{1,3}(?:,\\d{3})*|\\d+)/);\n  if (!match) return 0;\n  return parseInt(match[1].replace(/,/g, ''), 10);\n}\nconst buyerDeals = extractNumberAfter(\"Buyer Deals\");\nconst sellerDeals = extractNumberAfter(\"Seller Deals\");\nreturn [\n  {\n    json: {\n      Deals_1Y_Buyer: buyerDeals,\n      Deals_1Y_Seller: sellerDeals,\n      Deals_1Y_Total: buyerDeals + sellerDeals\n    }\n  }\n];"
      }
    },
    {
      "name": "Update Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 3,
      "position": [
        1250,
        0
      ],
      "parameters": {
        "operation": "update",
        "sheetName": "",
        "updateKey": "Email",
        "keyValue": "={{$node[\"SplitInBatches\"].json[\"Email\"]}}",
        "dataToSend": {
          "fields": {
            "Deals_1Y_Buyer": "={{$json[\"Deals_1Y_Buyer\"]}}",
            "Deals_1Y_Seller": "={{$json[\"Deals_1Y_Seller\"]}}",
            "Deals_1Y_Total": "={{$json[\"Deals_1Y_Total\"]}}"
          }
        }
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1500,
        0
      ],
      "parameters": {
        "waitTime": 2000
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Read Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Sheet": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Extract Deals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Deals": {
      "main": [
        [
          {
            "node": "Update Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Sheet": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}