{
  "name": "1 - Lead Intake",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-intake",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const raw = $input.first().json;\nconst input = raw.body || raw;\n\nconst name = (input.name || input.Name || input.full_name || '').toString().trim();\nconst phone = (input.phone || input.Phone || '').toString().replace(/[^\\d+]/g, '');\nconst email = (input.email || input.Email || '').toString().toLowerCase().trim();\nconst message = (input.message || input.Message || '').toString().trim();\nconst source = (input.source || input.Source || 'Website Form').toString().trim();\n\nif (!name || name.length < 2) throw new Error('Name is required');\nif (!phone && !email) throw new Error('Phone or email is required');\nif (phone && phone.length < 10) throw new Error('Invalid phone number');\nif (email && !email.includes('@')) throw new Error('Invalid email address');\n\nreturn { json: { name, phone, email, source, message, timestamp: new Date().toISOString() } };"
      },
      "id": "b2c3d4e5-f6a7-8901-bcde-f23456789012",
      "name": "Normalize Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.airtable.com/v0/YOUR_AIRTABLE_BASE_ID/Contacts",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "airtableTokenApi",
        "options": {
          "qs": {
            "filterByFormula": "=OR(AND(LEN(\"{{ $json.phone }}\")>0, {Phone}=\"{{ $json.phone }}\"), AND(LEN(\"{{ $json.email }}\")>0, {Email}=\"{{ $json.email }}\"))"
          }
        }
      },
      "id": "c3d4e5f6-a7b8-9012-cdef-345678901234",
      "name": "Check Duplicate",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "leftValue": "={{ $json.records.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ]
        }
      },
      "id": "d4e5f6a7-b8c9-0123-defa-456789012345",
      "name": "Exists?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const record = $('Check Duplicate').item.json.records[0];\nconst lead = $('Normalize Data').item.json;\nconst note = '[' + lead.timestamp + '] Returned via ' + lead.source + ': ' + lead.message;\n\nconst fields = {\n  InteractionCount: (record.fields.InteractionCount || 0) + 1,\n  LastSeen: lead.timestamp,\n  Notes: (record.fields.Notes || '') + (record.fields.Notes ? ' | ' : '') + note\n};\n\nif (!record.fields.Name && lead.name) fields.Name = lead.name;\nif (!record.fields.Phone && lead.phone) fields.Phone = lead.phone;\nif (!record.fields.Email && lead.email) fields.Email = lead.email;\nif (!record.fields.Source && lead.source) fields.Source = lead.source;\n\nreturn { json: { recordId: record.id, body: { fields }, isReturning: true } };"
      },
      "id": "prep-update-1111-1111-111111111111",
      "name": "Prep Update",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        200
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "=https://api.airtable.com/v0/YOUR_AIRTABLE_BASE_ID/Contacts/{{ $json.recordId }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "airtableTokenApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.body) }}"
      },
      "id": "e5f6a7b8-c9d0-1234-efab-567890123456",
      "name": "Update Contact",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        200
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const lead = $('Normalize Data').item.json;\n\nreturn {\n  json: {\n    body: {\n      fields: {\n        Name: lead.name,\n        Phone: lead.phone,\n        Email: lead.email,\n        Source: lead.source,\n        Status: 'New',\n        LeadScore: 0,\n        InteractionCount: 1,\n        LastSeen: lead.timestamp,\n        Notes: '[' + lead.timestamp + '] Initial inquiry: ' + lead.message\n      }\n    },\n    isReturning: false\n  }\n};"
      },
      "id": "prep-create-2222-2222-222222222222",
      "name": "Prep Create",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        400
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.airtable.com/v0/YOUR_AIRTABLE_BASE_ID/Contacts",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "airtableTokenApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.body) }}"
      },
      "id": "f6a7b8c9-d0e1-2345-fabc-678901234567",
      "name": "Create Contact",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        400
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const item = $input.first().json;\nconst lead = $('Normalize Data').item.json;\n\n// Build scoring data with all required fields\nconst scoringData = {\n  contact_id: item.id,\n  contact_name: item.fields.Name || lead.name,\n  lead_data: {\n    name: item.fields.Name || lead.name,\n    phone: item.fields.Phone || lead.phone,\n    email: item.fields.Email || lead.email,\n    source: item.fields.Source || lead.source,\n    message: lead.message,\n    interaction_count: item.fields.InteractionCount || 1\n  }\n};\n\nreturn { json: scoringData };"
      },
      "id": "b8c9d0e1-f2a3-4567-bcde-890123456789",
      "name": "Prep Scoring",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1560,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.airtable.com/v0/YOUR_AIRTABLE_BASE_ID/Interactions",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "airtableTokenApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ fields: { Contact: [$json.contact_id], Type: 'Form Submission', Direction: 'Inbound', Summary: 'New lead from ' + $json.lead_data.source, Details: $json.lead_data.message, Timestamp: new Date().toISOString(), Outcome: 'Completed' } }) }}"
      },
      "id": "log-interaction-1111-1111-111111111111",
      "name": "Log Interaction",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1780,
        200
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, message: 'Lead received', contact_id: $('Prep Scoring').item.json.contact_id }) }}"
      },
      "id": "d0e1f2a3-b4c5-6789-defa-012345678901",
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        2000,
        200
      ]
    },
    {
      "parameters": {
        "workflowId": {
          "__rl": true,
          "mode": "name",
          "value": "2 - Lead Scoring"
        },
        "options": {
          "waitForSubWorkflow": false
        }
      },
      "id": "c9d0e1f2-a3b4-5678-cdef-901234567890",
      "name": "Trigger Scoring",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.1,
      "position": [
        1780,
        400
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Normalize Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Data": {
      "main": [
        [
          {
            "node": "Check Duplicate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Duplicate": {
      "main": [
        [
          {
            "node": "Exists?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Exists?": {
      "main": [
        [
          {
            "node": "Prep Update",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prep Create",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prep Update": {
      "main": [
        [
          {
            "node": "Update Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Contact": {
      "main": [
        [
          {
            "node": "Prep Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prep Create": {
      "main": [
        [
          {
            "node": "Create Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Contact": {
      "main": [
        [
          {
            "node": "Prep Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prep Scoring": {
      "main": [
        [
          {
            "node": "Log Interaction",
            "type": "main",
            "index": 0
          },
          {
            "node": "Trigger Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Interaction": {
      "main": [
        [
          {
            "node": "Respond",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "versionId": "6"
}