{
  "name": "Galaxy IT \u2014 Monthly Client Report (GHL v1 + GSC + AI)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtDay": 1,
              "triggerAtHour": 9
            }
          ]
        }
      },
      "id": "schedule",
      "name": "Every 1st at 9AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        200,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "// ===== YOUR CLIENTS \u2014 EDIT THIS =====\n// Each client has their OWN GHL API key\nconst clients = [\n  {\n    id: 'alfa',\n    name: 'Alfa Construction Inc.',\n    ghlKey: 'YOUR_GHL_KEY',\n    email: 'info@alfapaintingcarpentry.com',\n    phone: '+15085963750',\n    site: 'https://www.alfapaintingcarpentry.com/',\n    language: 'en'\n  },\n  // ADD MORE CLIENTS:\n  // { id: 'client2', name: '...', ghlKey: '...', email: '...', phone: '+1...', site: 'https://...', language: 'en' },\n];\n\nreturn clients.map(c => ({ json: c }));"
      },
      "id": "clients",
      "name": "Client List (EDIT THIS)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        420,
        400
      ]
    },
    {
      "parameters": {
        "url": "=https://rest.gohighlevel.com/v1/contacts/?limit=100",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $json.ghlKey }}"
            }
          ]
        },
        "options": {
          "timeout": 15000
        }
      },
      "id": "ghl-contacts",
      "name": "GHL \u2014 Get Contacts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "url": "=https://rest.gohighlevel.com/v1/pipelines/",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $json.ghlKey }}"
            }
          ]
        },
        "options": {
          "timeout": 15000
        }
      },
      "id": "ghl-pipes",
      "name": "GHL \u2014 Get Pipelines",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        500
      ]
    },
    {
      "parameters": {
        "jsCode": "const client = $('Client List (EDIT THIS)').item.json;\nconst contactsData = $('GHL \u2014 Get Contacts').item.json;\nconst pipelinesData = $('GHL \u2014 Get Pipelines').item.json;\n\nconst contacts = contactsData.contacts || [];\nconst sources = {};\ncontacts.forEach(c => {\n  const s = c.source || c.tags?.[0] || 'Direct';\n  sources[s] = (sources[s] || 0) + 1;\n});\n\nconst pipelines = pipelinesData.pipelines || [];\nconst pipeInfo = pipelines.map(p => p.name + ' (' + p.stages.length + ' stages)').join(', ');\n\nconst now = new Date();\nreturn [{ json: {\n  ...client,\n  totalContacts: contacts.length,\n  leadSources: JSON.stringify(sources),\n  pipelines: pipeInfo,\n  monthName: now.toLocaleString('en-US', { month: 'long', year: 'numeric' }),\n  period: new Date(now.getTime()-30*24*60*60*1000).toISOString().split('T')[0] + ' to ' + now.toISOString().split('T')[0]\n}}];"
      },
      "id": "merge",
      "name": "Process Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        400
      ]
    },
    {
      "parameters": {
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.GROQ_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"llama-3.3-70b-versatile\",\n  \"messages\": [{\n    \"role\": \"user\",\n    \"content\": \"You are a marketing report writer for Galaxy IT & Marketing. Write a monthly performance report for '{{ $json.name }}'.\\n\\nWrite in {{ $json.language === 'pt' ? 'Brazilian Portuguese' : $json.language === 'es' ? 'Spanish' : 'English' }}.\\n\\nDATA ({{ $json.period }}):\\n- Contacts in CRM: {{ $json.totalContacts }}\\n- Lead sources: {{ $json.leadSources }}\\n- Pipelines: {{ $json.pipelines }}\\n- Website: {{ $json.site }}\\n\\nFORMAT:\\n1. Greeting (1 sentence, use client name)\\n2. Key Highlights (3-4 bullets with numbers)\\n3. Lead Performance (which sources bring most leads)\\n4. One actionable recommendation\\n5. Sign off as Galaxy IT & Marketing\\n\\nMax 200 words. Professional but warm. Use specific numbers.\"\n  }],\n  \"max_tokens\": 600,\n  \"temperature\": 0.7\n}",
        "options": {
          "timeout": 25000
        }
      },
      "id": "ai",
      "name": "AI \u2014 Write Report",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "const client = $('Process Data').item.json;\nconst report = $json.choices[0].message.content;\nconst monthName = client.monthName;\nconst reportHtml = report.replace(/\\n/g, '<br>').replace(/\\*\\*(.*?)\\*\\*/g, '<b>$1</b>');\n\nconst html = `<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body style=\"font-family:Inter,Arial,sans-serif;max-width:600px;margin:0 auto;padding:20px;color:#333;\">\n<div style=\"background:linear-gradient(135deg,#0d1b2a,#1a237e);padding:30px;border-radius:12px 12px 0 0;text-align:center;\">\n<h1 style=\"color:#ffd700;margin:0;font-size:22px;\">Galaxy IT & Marketing</h1>\n<p style=\"color:rgba(255,255,255,0.7);margin:8px 0 0;font-size:14px;\">Monthly Report \u2014 ${monthName}</p>\n</div>\n<div style=\"background:#fff;padding:30px;border:1px solid #e5e5e5;border-top:none;\">\n<p style=\"font-size:14px;line-height:1.8;\">${reportHtml}</p>\n</div>\n<div style=\"background:#f5f5f5;padding:20px;border-radius:0 0 12px 12px;text-align:center;border:1px solid #e5e5e5;border-top:none;\">\n<p style=\"margin:0;font-size:12px;color:#888;\">Questions? <a href=\"https://galaxyinfo.us/support\" style=\"color:#1a237e;\">Help Center</a></p>\n<p style=\"margin:8px 0 0;font-size:11px;color:#aaa;\">&copy; 2026 Galaxy IT & Marketing</p>\n</div></body></html>`;\n\nlet wa = '\\ud83d\\udcca *Monthly Report \u2014 ' + monthName + '*\\n\\n';\nwa += '\\ud83d\\udc65 Contacts: ' + client.totalContacts + '\\n';\nwa += '\\ud83d\\udcc8 Sources: ' + client.leadSources + '\\n\\n';\nwa += 'Full report sent to your email!';\n\nreturn [{ json: { ...client, report, html, wa, monthName } }];"
      },
      "id": "format",
      "name": "Format Email + WhatsApp",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1340,
        400
      ]
    },
    {
      "parameters": {
        "url": "=https://rest.gohighlevel.com/v1/contacts/lookup?email={{ encodeURIComponent($json.email) }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $json.ghlKey }}"
            }
          ]
        },
        "options": {}
      },
      "id": "find",
      "name": "GHL \u2014 Find Contact",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        400
      ]
    },
    {
      "parameters": {
        "url": "https://rest.gohighlevel.com/v1/conversations/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Format Email + WhatsApp').item.json.ghlKey }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"type\": \"Email\",\n  \"contactId\": \"{{ $json.contact?.id || $json.contacts?.[0]?.id }}\",\n  \"subject\": \"Monthly Performance Report \u2014 {{ $('Format Email + WhatsApp').item.json.monthName }} | Galaxy IT\",\n  \"html\": {{ JSON.stringify($('Format Email + WhatsApp').item.json.html) }}\n}",
        "options": {}
      },
      "id": "email",
      "name": "Send Email Report",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1780,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://rest.gohighlevel.com/v1/conversations/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Format Email + WhatsApp').item.json.ghlKey }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"type\": \"WhatsApp\",\n  \"contactId\": \"{{ $json.contact?.id || $json.contacts?.[0]?.id }}\",\n  \"message\": \"{{ $('Format Email + WhatsApp').item.json.wa }}\"\n}",
        "options": {}
      },
      "id": "whatsapp",
      "name": "Send WhatsApp Summary",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1780,
        500
      ]
    }
  ],
  "connections": {
    "Every 1st at 9AM": {
      "main": [
        [
          {
            "node": "Client List (EDIT THIS)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Client List (EDIT THIS)": {
      "main": [
        [
          {
            "node": "GHL \u2014 Get Contacts",
            "type": "main",
            "index": 0
          },
          {
            "node": "GHL \u2014 Get Pipelines",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GHL \u2014 Get Contacts": {
      "main": [
        [
          {
            "node": "Process Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GHL \u2014 Get Pipelines": {
      "main": [
        [
          {
            "node": "Process Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Data": {
      "main": [
        [
          {
            "node": "AI \u2014 Write Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI \u2014 Write Report": {
      "main": [
        [
          {
            "node": "Format Email + WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Email + WhatsApp": {
      "main": [
        [
          {
            "node": "GHL \u2014 Find Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GHL \u2014 Find Contact": {
      "main": [
        [
          {
            "node": "Send Email Report",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send WhatsApp Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}