{
  "name": "01 - AI Lead Qualification & CRM Sync",
  "nodes": [
    {
      "id": "node-1",
      "name": "Webhook - Capture Lead",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300],
      "parameters": { "httpMethod": "POST", "path": "lead-capture", "responseMode": "responseNode" }
    },
    {
      "id": "node-2",
      "name": "OpenAI - Score Lead",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1.8,
      "position": [480, 300],
      "credentials": { "openAiApi": { "id": "YOUR_OPENAI_CREDENTIAL_ID", "name": "OpenAI account" } },
      "parameters": {
        "resource": "chat",
        "operation": "complete",
        "modelId": { "__rl": true, "value": "gpt-4o", "mode": "list", "cachedResultName": "gpt-4o" },
        "messages": {
          "values": [
            { "role": "system", "content": "You are a B2B lead scoring expert. Score leads from 1-100. Return ONLY valid JSON: {\"score\": number, \"reason\": string, \"priority\": \"high|medium|low\"}" },
            { "role": "user", "content": "=Score this lead: Name={{ $json.name }}, Company={{ $json.company }}, Job Title={{ $json.job_title }}, Company Size={{ $json.company_size }}, Industry={{ $json.industry }}" }
          ]
        },
        "options": { "temperature": 0.3 }
      }
    },
    {
      "id": "node-3",
      "name": "Code - Parse Score",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [720, 300],
      "parameters": {
        "jsCode": "const ai = JSON.parse($input.first().json.message.content);\nconst lead = $('Webhook - Capture Lead').first().json;\nreturn [{ json: { ...lead, score: ai.score, reason: ai.reason, priority: ai.priority, scored_at: new Date().toISOString() } }];"
      }
    },
    {
      "id": "node-4",
      "name": "Clearbit - Enrich Person",
      "type": "n8n-nodes-base.clearbit",
      "typeVersion": 1,
      "position": [960, 300],
      "credentials": { "clearbitApi": { "id": "YOUR_CLEARBIT_CREDENTIAL_ID", "name": "Clearbit account" } },
      "parameters": { "resource": "person", "operation": "enrich", "email": "={{ $json.email }}" }
    },
    {
      "id": "node-5",
      "name": "IF - Score Above 70",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1200, 300],
      "parameters": {
        "conditions": {
          "conditions": [{ "id": "c1", "leftValue": "={{ $json.score }}", "rightValue": 70, "operator": { "type": "number", "operation": "gt" } }],
          "combinator": "and"
        }
      }
    },
    {
      "id": "node-6",
      "name": "HubSpot - Create or Update Contact",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2,
      "position": [1440, 160],
      "credentials": { "hubspotApi": { "id": "YOUR_HUBSPOT_CREDENTIAL_ID", "name": "HubSpot account" } },
      "parameters": {
        "resource": "contact",
        "operation": "upsert",
        "additionalFields": {
          "email": "={{ $json.email }}",
          "properties": [
            { "name": "firstname", "value": "={{ $json.name.split(' ')[0] }}" },
            { "name": "lastname", "value": "={{ $json.name.split(' ').slice(1).join(' ') }}" },
            { "name": "company", "value": "={{ $json.company }}" },
            { "name": "jobtitle", "value": "={{ $json.job_title }}" }
          ]
        }
      }
    },
    {
      "id": "node-7",
      "name": "Gmail - Send Welcome Email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [1680, 160],
      "credentials": { "gmailOAuth2": { "id": "YOUR_GMAIL_CREDENTIAL_ID", "name": "Gmail account" } },
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $json.email }}",
        "subject": "=Thanks for reaching out, {{ $json.name.split(' ')[0] }}!",
        "message": "=<h2>Hi {{ $json.name.split(' ')[0] }},</h2><p>Thank you for your interest! A member of our team will be in touch within 24 hours.</p><p>In the meantime, feel free to <a href='https://calendly.com/yourteam/demo'>book a demo</a>.</p>",
        "options": { "isHtml": true }
      }
    },
    {
      "id": "node-8",
      "name": "Slack - Notify Sales Team",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [1440, 420],
      "credentials": { "slackApi": { "id": "YOUR_SLACK_CREDENTIAL_ID", "name": "Slack account" } },
      "parameters": {
        "resource": "message",
        "operation": "send",
        "channel": { "__rl": true, "value": "#sales-leads", "mode": "name" },
        "text": "=🔥 Hot Lead!\n*Name:* {{ $json.name }}\n*Company:* {{ $json.company }}\n*Score:* {{ $json.score }}/100 ({{ $json.priority }})\n*Reason:* {{ $json.reason }}",
        "otherOptions": {}
      }
    },
    {
      "id": "node-9",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [1920, 160],
      "parameters": { "respondWith": "json", "responseBody": "={ \"status\": \"received\" }" }
    }
  ],
  "connections": {
    "Webhook - Capture Lead": { "main": [[{ "node": "OpenAI - Score Lead", "type": "main", "index": 0 }]] },
    "OpenAI - Score Lead": { "main": [[{ "node": "Code - Parse Score", "type": "main", "index": 0 }]] },
    "Code - Parse Score": { "main": [[{ "node": "Clearbit - Enrich Person", "type": "main", "index": 0 }]] },
    "Clearbit - Enrich Person": { "main": [[{ "node": "IF - Score Above 70", "type": "main", "index": 0 }]] },
    "IF - Score Above 70": {
      "main": [
        [{ "node": "HubSpot - Create or Update Contact", "type": "main", "index": 0 }, { "node": "Slack - Notify Sales Team", "type": "main", "index": 0 }],
        []
      ]
    },
    "HubSpot - Create or Update Contact": { "main": [[{ "node": "Gmail - Send Welcome Email", "type": "main", "index": 0 }]] },
    "Gmail - Send Welcome Email": { "main": [[{ "node": "Respond to Webhook", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "active": false,
  "tags": [{ "name": "Portfolio" }, { "name": "Sales" }, { "name": "AI" }]
}
