{
  "name": "01 — Sourcing (GMaps daily)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 3 * * *"
            }
          ]
        }
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000001",
      "name": "Daily 03:00 Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const raw = $env.NICHE_QUERIES || '[\"boutique hotel Milan\",\"architectural studio Barcelona\",\"B&B Lake Como\"]';\nconst queries = JSON.parse(raw);\nreturn queries.map(q => ({ json: { niche_query: q } }));"
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000002",
      "name": "Load Niche Queries",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000003",
      "name": "Split Queries",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "http://playwright-worker:8000/scrape-gmaps",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "query",
              "value": "={{ $json.niche_query }}"
            },
            {
              "name": "limit",
              "value": "50"
            }
          ]
        },
        "options": {
          "timeout": 120000
        }
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000004",
      "name": "Scrape Google Maps",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const results = $input.item.json.results || [];\nconst niche_query = $input.item.json.niche_query || '';\nreturn results\n  .filter(r => r.domain)\n  .map(r => ({ json: { ...r, niche_query } }));"
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000005",
      "name": "Flatten Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000006",
      "name": "Split Results",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id FROM leads.leads WHERE domain = $1 LIMIT 1",
        "additionalFields": {
          "queryParams": "={{ JSON.stringify([$json.domain]) }}"
        }
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000007",
      "name": "Check Domain Exists",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1560,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "check-new",
              "leftValue": "={{ $json.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "empty"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000008",
      "name": "Is New Domain?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1780,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO leads.leads (source, company_name, domain, website, country, city, phone, rating, review_count, state, niche_query)\nVALUES ('gmaps', $1, $2, $3, $4, $5, $6, $7, $8, 'new', $9)\nON CONFLICT (domain) DO NOTHING",
        "additionalFields": {
          "queryParams": "={{ JSON.stringify([\n  $('Flatten Results').item.json.company_name,\n  $('Flatten Results').item.json.domain,\n  $('Flatten Results').item.json.website,\n  $('Flatten Results').item.json.country,\n  $('Flatten Results').item.json.city,\n  $('Flatten Results').item.json.phone,\n  $('Flatten Results').item.json.rating,\n  $('Flatten Results').item.json.review_count,\n  $('Flatten Results').item.json.niche_query\n]) }}"
        }
      },
      "id": "a1b2c3d4-0001-0001-0001-000000000009",
      "name": "Insert New Lead",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        2000,
        200
      ]
    },
    {
      "parameters": {},
      "id": "a1b2c3d4-0001-0001-0001-000000000010",
      "name": "Already Exists (skip)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        2000,
        420
      ]
    }
  ],
  "connections": {
    "Daily 03:00 Trigger": {
      "main": [
        [
          {
            "node": "Load Niche Queries",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load Niche Queries": {
      "main": [
        [
          {
            "node": "Split Queries",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Queries": {
      "main": [
        [],
        [
          {
            "node": "Scrape Google Maps",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Scrape Google Maps": {
      "main": [
        [
          {
            "node": "Flatten Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatten Results": {
      "main": [
        [
          {
            "node": "Split Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Results": {
      "main": [
        [],
        [
          {
            "node": "Check Domain Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Domain Exists": {
      "main": [
        [
          {
            "node": "Is New Domain?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is New Domain?": {
      "main": [
        [
          {
            "node": "Insert New Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Already Exists (skip)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert New Lead": {
      "main": [
        [
          {
            "node": "Split Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
