{
  "name": "Weekly Business Summary Email",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8,
              "triggerAtMinute": 0,
              "triggerAtDay": 1
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Every Monday 8am",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [220, 300]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Metrics",
          "mode": "name"
        },
        "options": {
          "range": "A:E"
        }
      },
      "id": "read-metrics-sheet",
      "name": "Read Metrics Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [440, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Calculate this week's totals and compare to last week\nconst items = $input.all();\n\n// Get current date info\nconst today = new Date();\nconst oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);\nconst twoWeeksAgo = new Date(today.getTime() - 14 * 24 * 60 * 60 * 1000);\n\n// Parse dates and filter by week\nfunction parseDate(dateStr) {\n  if (!dateStr) return null;\n  return new Date(dateStr);\n}\n\nfunction isThisWeek(date) {\n  return date >= oneWeekAgo && date <= today;\n}\n\nfunction isLastWeek(date) {\n  return date >= twoWeeksAgo && date < oneWeekAgo;\n}\n\n// Initialize counters\nlet thisWeekRevenue = 0;\nlet lastWeekRevenue = 0;\nlet thisWeekLeads = 0;\nlet lastWeekLeads = 0;\nlet thisWeekJobs = 0;\nlet lastWeekJobs = 0;\n\n// Process each row\nfor (const item of items) {\n  const data = item.json;\n  const date = parseDate(data.Date || data.date);\n  \n  if (!date) continue;\n  \n  const revenue = parseFloat(data.Revenue || data.revenue || 0);\n  const leads = parseInt(data.Leads || data.leads || 0);\n  const jobs = parseInt(data.Jobs || data.jobs_completed || data.Jobs_Completed || 0);\n  \n  if (isThisWeek(date)) {\n    thisWeekRevenue += revenue;\n    thisWeekLeads += leads;\n    thisWeekJobs += jobs;\n  } else if (isLastWeek(date)) {\n    lastWeekRevenue += revenue;\n    lastWeekLeads += leads;\n    lastWeekJobs += jobs;\n  }\n}\n\n// Calculate changes\nfunction calcChange(thisWeek, lastWeek) {\n  if (lastWeek === 0) return thisWeek > 0 ? '+100%' : '0%';\n  const change = ((thisWeek - lastWeek) / lastWeek * 100).toFixed(0);\n  return change >= 0 ? `+${change}%` : `${change}%`;\n}\n\nconst revenueChange = calcChange(thisWeekRevenue, lastWeekRevenue);\nconst leadsChange = calcChange(thisWeekLeads, lastWeekLeads);\nconst jobsChange = calcChange(thisWeekJobs, lastWeekJobs);\n\n// Format currency\nfunction formatCurrency(amount) {\n  return '$' + amount.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });\n}\n\nreturn [{\n  json: {\n    thisWeekRevenue: formatCurrency(thisWeekRevenue),\n    lastWeekRevenue: formatCurrency(lastWeekRevenue),\n    revenueChange,\n    thisWeekLeads,\n    lastWeekLeads,\n    leadsChange,\n    thisWeekJobs,\n    lastWeekJobs,\n    jobsChange,\n    weekEnding: today.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })\n  }\n}];"
      },
      "id": "calculate-metrics",
      "name": "Calculate Weekly Metrics",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 300]
    },
    {
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "subject",
              "name": "subject",
              "value": "=Weekly Business Summary - Week Ending {{ $json.weekEnding }}",
              "type": "string"
            },
            {
              "id": "emailBody",
              "name": "emailBody",
              "value": "=<div style=\"font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;\">\n  <h1 style=\"color: #333; border-bottom: 2px solid #007bff; padding-bottom: 10px;\">Weekly Business Summary</h1>\n  <p style=\"color: #666;\">Week ending {{ $json.weekEnding }}</p>\n  \n  <table style=\"width: 100%; border-collapse: collapse; margin: 20px 0;\">\n    <tr style=\"background: #f8f9fa;\">\n      <th style=\"padding: 12px; text-align: left; border-bottom: 2px solid #dee2e6;\">Metric</th>\n      <th style=\"padding: 12px; text-align: right; border-bottom: 2px solid #dee2e6;\">This Week</th>\n      <th style=\"padding: 12px; text-align: right; border-bottom: 2px solid #dee2e6;\">Last Week</th>\n      <th style=\"padding: 12px; text-align: right; border-bottom: 2px solid #dee2e6;\">Change</th>\n    </tr>\n    <tr>\n      <td style=\"padding: 12px; border-bottom: 1px solid #dee2e6;\"><strong>Revenue</strong></td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6;\">{{ $json.thisWeekRevenue }}</td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6;\">{{ $json.lastWeekRevenue }}</td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6; color: {{ $json.revenueChange.startsWith('+') ? '#28a745' : '#dc3545' }};\">{{ $json.revenueChange }}</td>\n    </tr>\n    <tr>\n      <td style=\"padding: 12px; border-bottom: 1px solid #dee2e6;\"><strong>New Leads</strong></td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6;\">{{ $json.thisWeekLeads }}</td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6;\">{{ $json.lastWeekLeads }}</td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6; color: {{ $json.leadsChange.startsWith('+') ? '#28a745' : '#dc3545' }};\">{{ $json.leadsChange }}</td>\n    </tr>\n    <tr>\n      <td style=\"padding: 12px; border-bottom: 1px solid #dee2e6;\"><strong>Jobs Completed</strong></td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6;\">{{ $json.thisWeekJobs }}</td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6;\">{{ $json.lastWeekJobs }}</td>\n      <td style=\"padding: 12px; text-align: right; border-bottom: 1px solid #dee2e6; color: {{ $json.jobsChange.startsWith('+') ? '#28a745' : '#dc3545' }};\">{{ $json.jobsChange }}</td>\n    </tr>\n  </table>\n  \n  <p style=\"color: #666; font-size: 14px; margin-top: 30px; padding-top: 20px; border-top: 1px solid #dee2e6;\">This summary was automatically generated by your n8n workflow. Reply to this email if you'd like to adjust what metrics are tracked.</p>\n</div>",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "format-email",
      "name": "Format Email Content",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [880, 300]
    },
    {
      "parameters": {
        "sendTo": "={{ $env.OWNER_EMAIL || 'your-email@example.com' }}",
        "subject": "={{ $json.subject }}",
        "emailType": "html",
        "message": "={{ $json.emailBody }}",
        "options": {}
      },
      "id": "send-email",
      "name": "Send Summary Email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [1100, 300],
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CREDENTIAL_ID",
          "name": "Gmail account"
        }
      }
    }
  ],
  "connections": {
    "Every Monday 8am": {
      "main": [
        [
          {
            "node": "Read Metrics Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Metrics Sheet": {
      "main": [
        [
          {
            "node": "Calculate Weekly Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Weekly Metrics": {
      "main": [
        [
          {
            "node": "Format Email Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Email Content": {
      "main": [
        [
          {
            "node": "Send Summary Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "errorWorkflow": ""
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "uzunu-automation-templates"
  },
  "pinData": {},
  "versionId": "1",
  "triggerCount": 1,
  "tags": [
    {
      "name": "reporting",
      "id": "reporting"
    },
    {
      "name": "google-sheets",
      "id": "google-sheets"
    },
    {
      "name": "gmail",
      "id": "gmail"
    },
    {
      "name": "scheduled",
      "id": "scheduled"
    }
  ]
}
