{
  "name": "Hotline — Owner Notification",
  "meta": {
    "description": "Hot leads trigger an instant SMS + Slack message to the owner with the score, the evidence phrases, and a one-tap callback link."
  },
  "nodes": [
    {
      "id": "d1",
      "name": "Trigger: Lead Qualified",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 300],
      "parameters": { "path": "hotline-owner-ping", "httpMethod": "POST" },
      "notes": "Called by instant-response.json right after the confidence gate."
    },
    {
      "id": "d2",
      "name": "Band Router",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [220, 300],
      "parameters": {
        "rules": {
          "values": [
            { "conditions": { "string": [{ "value1": "={{ $json.band }}", "value2": "hot" }] }, "outputKey": "hot" },
            { "conditions": { "string": [{ "value1": "={{ $json.band }}", "value2": "warm" }] }, "outputKey": "warm" }
          ]
        },
        "fallbackOutput": "extra"
      },
      "notes": "HOT → SMS + Slack + callback link. WARM → Slack only. COLD → logged, no interruption. The owner's phone only buzzes when money is on the table."
    },
    {
      "id": "d3",
      "name": "SMS (Twilio)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 140],
      "parameters": {
        "method": "POST",
        "url": "=https://api.twilio.com/2010-04-01/Accounts/{{ $env.TWILIO_SID }}/Messages.json",
        "sendBody": true,
        "contentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            { "name": "To", "value": "={{ $env.OWNER_PHONE }}" },
            { "name": "From", "value": "={{ $env.TWILIO_FROM }}" },
            { "name": "Body", "value": "=🔥 HOT {{ $json.score }}/100: {{ $json.name }} ({{ $json.service }}). Said: \"{{ $json.evidence.join('\", \"') }}\". Reply sent. Call back: tel:{{ $json.phone }}" }
          ]
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000,
      "notes": "RETRY RULE: 3 attempts with backoff; on final failure falls back to Slack so the alert is never silently dropped."
    },
    {
      "id": "d4",
      "name": "Slack Card",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 380],
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "sendBody": true,
        "jsonBody": "={\"blocks\": [{\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": \"*{{ $json.band.toUpperCase() }}* lead · *{{ $json.score }}/100*\\n{{ $json.name }} — {{ $json.service }}\\n> {{ $json.message }}\\nEvidence: `{{ $json.evidence.join('` `') }}`\\nAI reply: {{ $json.emailStatus }} in {{ $json.elapsedMs/1000 }}s\"}}, {\"type\": \"actions\", \"elements\": [{\"type\": \"button\", \"text\": {\"type\": \"plain_text\", \"text\": \"Open in CRM\"}, \"url\": \"{{ $env.APP_URL }}/crm\"}]}]}"
      },
      "retryOnFail": true,
      "maxTries": 2,
      "waitBetweenTries": 1000
    },
    {
      "id": "d5",
      "name": "Log Notification",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [700, 300],
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO runs (id, lead_id, status, duration_ms, steps) VALUES (gen_random_uuid()::text, $1, 'success', $2, $3)",
        "options": { "queryReplacement": "={{ [$json.leadId, $json.elapsedMs, $json.steps] }}" }
      }
    }
  ],
  "connections": {
    "Trigger: Lead Qualified": { "main": [[{ "node": "Band Router", "type": "main", "index": 0 }]] },
    "Band Router": {
      "main": [
        [{ "node": "SMS (Twilio)", "type": "main", "index": 0 }],
        [{ "node": "Slack Card", "type": "main", "index": 0 }]
      ]
    },
    "SMS (Twilio)": { "main": [[{ "node": "Log Notification", "type": "main", "index": 0 }]] },
    "Slack Card": { "main": [[{ "node": "Log Notification", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1", "timezone": "America/Montreal" }
}
