{
  "name": "Hotline — Enrichment",
  "meta": {
    "description": "Fills in what the lead didn't say: geo from IP, service inference from message text, duplicate detection against the last 90 days of leads."
  },
  "nodes": [
    {
      "id": "b1",
      "name": "Trigger: New Intake",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 300],
      "parameters": { "path": "hotline-enrich", "httpMethod": "POST" }
    },
    {
      "id": "b2",
      "name": "Geo from IP",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 300],
      "parameters": {
        "url": "=https://ipapi.co/{{ $json.ip }}/json/",
        "options": { "timeout": 3000 }
      },
      "retryOnFail": true,
      "maxTries": 2,
      "waitBetweenTries": 1000,
      "notes": "Non-blocking: if geo fails, the pipeline continues with city = null. Enrichment must never delay the response past the 60s budget."
    },
    {
      "id": "b3",
      "name": "Infer Service from Text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 300],
      "parameters": {
        "jsCode": "// When the lead picked 'other', infer the trade from the message.\n// Mirrors lib/engine.ts SERVICE_HINTS in the demo app.\nconst hints = {\n  plumbing: /plumb|pipe|leak|faucet|water heater|plomb|tuyau|fuite|robinet/i,\n  hvac: /hvac|furnace|heating|air condition|chauffage|climatisation|fournaise/i,\n  roofing: /roof|shingle|gutter|toit|toiture|bardeau/i,\n  electrical: /electr|panel|breaker|électr|panneau|disjoncteur/i,\n};\nfor (const item of $input.all()) {\n  if (item.json.service === 'other') {\n    for (const [service, re] of Object.entries(hints)) {\n      if (re.test(item.json.message)) { item.json.service = service; item.json.serviceInferred = true; break; }\n    }\n  }\n}\nreturn $input.all();"
      }
    },
    {
      "id": "b4",
      "name": "Seen in Last 90 Days?",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [660, 300],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id, stage, created_at FROM leads WHERE email = $1 AND created_at > NOW() - INTERVAL '90 days' ORDER BY created_at DESC",
        "options": { "queryReplacement": "={{ [$json.email] }}" }
      },
      "notes": "DEDUPE RULE: any prior lead with the same email in 90 days is attached as history. Same email + same service collapses into one deal (update), different service becomes a linked second deal. The pipeline never double-counts."
    },
    {
      "id": "b5",
      "name": "Merge or Create",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [880, 300],
      "parameters": {
        "conditions": {
          "boolean": [{ "value1": "={{ $json.length > 0 }}", "operation": "isTrue" }]
        }
      }
    },
    {
      "id": "b6",
      "name": "Update Existing Deal",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [1100, 180],
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE leads SET message = $2, stage = CASE WHEN stage IN ('won','lost') THEN stage ELSE 'new' END, created_at = NOW() WHERE id = $1",
        "options": { "queryReplacement": "={{ [$json.id, $json.message] }}" }
      }
    },
    {
      "id": "b7",
      "name": "Pass Through as New",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1100, 420],
      "parameters": { "assignments": { "assignments": [{ "id": "dedupe", "name": "dedupe", "value": "new-record", "type": "string" }] } }
    }
  ],
  "connections": {
    "Trigger: New Intake": { "main": [[{ "node": "Geo from IP", "type": "main", "index": 0 }]] },
    "Geo from IP": { "main": [[{ "node": "Infer Service from Text", "type": "main", "index": 0 }]] },
    "Infer Service from Text": { "main": [[{ "node": "Seen in Last 90 Days?", "type": "main", "index": 0 }]] },
    "Seen in Last 90 Days?": { "main": [[{ "node": "Merge or Create", "type": "main", "index": 0 }]] },
    "Merge or Create": {
      "main": [
        [{ "node": "Update Existing Deal", "type": "main", "index": 0 }],
        [{ "node": "Pass Through as New", "type": "main", "index": 0 }]
      ]
    }
  },
  "settings": { "executionOrder": "v1", "timezone": "America/Montreal" }
}
