{
  "name": "YA-RE-04: Lead Quality Scoring Agent",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-scoring-agent",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [220, 300],
      "id": "node-webhook-scoring",
      "name": "Webhook: Inbound Scoring Request"
    },
    {
      "parameters": {
        "jsCode": "// Deterministic Anonymization & Feature Extraction for Lead Scoring\nconst raw = $input.first().json.body || $input.first().json;\n\nconst leadId = raw.lead_id || `lead_${Date.now()}`;\nconst project = String(raw.project || raw.project_interest || 'General Off-Plan').trim();\nconst statedBudget = String(raw.budget || raw.budget_range || 'Not Specified').trim();\nconst inquiryMessage = String(raw.message || raw.inquiry_text || 'No comment provided').trim();\nconst paymentPreference = String(raw.payment_plan || raw.payment_preference || 'Standard').trim();\nconst utmCampaign = String(raw.utm_campaign || 'off_plan_launch');\n\n// Privacy-Safe Feature Vector: Zero PII (Name/Phone/Email omitted)\nconst scoringFeatures = {\n  project_interest: project,\n  stated_budget: statedBudget,\n  customer_inquiry: inquiryMessage,\n  payment_preference: paymentPreference,\n  campaign_context: utmCampaign\n};\n\nreturn [{\n  json: {\n    lead_id: leadId,\n    sanitized_features: JSON.stringify(scoringFeatures),\n    evaluated_at: new Date().toISOString()\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 300],
      "id": "node-code-feature-prep",
      "name": "Code: Privacy-Safe Feature Extraction"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You are an expert Real Estate Lead Qualification Specialist evaluating buyer intent for high-ticket property developments.\nAnalyze the following anonymized inquiry features:\n{{ $json.sanitized_features }}\n\nEvaluation Criteria:\n1. Classify buyer persona: 'INVESTOR' (focuses on ROI, capital appreciation, bulk units), 'END_USER' (focuses on living amenities, handover date, payment flexibility), 'BROKER_AGENT' (inquiries on commission splits, channel terms), or 'LOW_INTENT' (spam/generic).\n2. Calculate numerical quality score (0 to 100).\n3. Assign tier: 'TIER_A' (Score 80-100), 'TIER_B' (Score 60-79), 'TIER_C' (Score 40-59), or 'TIER_D' (Score < 40).\n4. Provide 1-sentence sales context justification.",
        "hasOutputParser": true
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [660, 300],
      "id": "node-ai-scoring-agent",
      "name": "AI Agent: Lead Quality Scoring"
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "options": {
          "temperature": 0.1
        }
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1,
      "position": [600, 520],
      "id": "node-openai-scoring-model",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "YOUR_OPENAI_CREDENTIAL_ID",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "jsonSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"buyer_persona\": {\n      \"type\": \"string\",\n      \"enum\": [\"INVESTOR\", \"END_USER\", \"BROKER_AGENT\", \"LOW_INTENT\"]\n    },\n    \"lead_score\": {\n      \"type\": \"number\",\n      \"minimum\": 0,\n      \"maximum\": 100\n    },\n    \"lead_tier\": {\n      \"type\": \"string\",\n      \"enum\": [\"TIER_A\", \"TIER_B\", \"TIER_C\", \"TIER_D\"]\n    },\n    \"intent_summary\": {\n      \"type\": \"string\"\n    },\n    \"recommended_sales_action\": {\n      \"type\": \"string\"\n    }\n  },\n  \"required\": [\"buyer_persona\", \"lead_score\", \"lead_tier\", \"intent_summary\", \"recommended_sales_action\"]\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.2,
      "position": [780, 520],
      "id": "node-scoring-parser",
      "name": "Structured Scoring Parser"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"status\": \"scored\",\n  \"lead_id\": \"{{ $('Code: Privacy-Safe Feature Extraction').item.json.lead_id }}\",\n  \"score\": {{ $json.output.lead_score }},\n  \"tier\": \"{{ $json.output.lead_tier }}\",\n  \"persona\": \"{{ $json.output.buyer_persona }}\",\n  \"summary\": \"{{ $json.output.intent_summary }}\",\n  \"recommended_action\": \"{{ $json.output.recommended_sales_action }}\",\n  \"evaluated_at\": \"{{ $('Code: Privacy-Safe Feature Extraction').item.json.evaluated_at }}\"\n}",
        "options": {
          "responseCode": 200
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [960, 300],
      "id": "node-respond-scoring",
      "name": "Respond: Lead Score Result"
    }
  ],
  "connections": {
    "Webhook: Inbound Scoring Request": {
      "main": [
        [
          {
            "node": "Code: Privacy-Safe Feature Extraction",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Privacy-Safe Feature Extraction": {
      "main": [
        [
          {
            "node": "AI Agent: Lead Quality Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent: Lead Quality Scoring",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Scoring Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent: Lead Quality Scoring",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent: Lead Quality Scoring": {
      "main": [
        [
          {
            "node": "Respond: Lead Score Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1.0.0",
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "yehia-ahmed-re-playbook"
  },
  "tags": [
    {
      "name": "AI Agent"
    },
    {
      "name": "Lead Scoring"
    },
    {
      "name": "Quality Classification"
    }
  ]
}
