{
  "name": "Daily Operations Briefing -> Executive Email",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * *"
            }
          ]
        }
      },
      "id": "brief-cron-trigger",
      "name": "Schedule Trigger - 08:00 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [200, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.codeven.io/v1/metrics/leads/yesterday",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{$env.INTERNAL_METRICS_API_KEY}}"
            }
          ]
        },
        "options": {}
      },
      "id": "brief-fetch-leads",
      "name": "HTTP - Fetch Yesterday Leads",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 160]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.codeven.io/v1/metrics/appointments/yesterday",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{$env.INTERNAL_METRICS_API_KEY}}"
            }
          ]
        },
        "options": {}
      },
      "id": "brief-fetch-appts",
      "name": "HTTP - Fetch Appointments",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.codeven.io/v1/metrics/support/yesterday",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{$env.INTERNAL_METRICS_API_KEY}}"
            }
          ]
        },
        "options": {}
      },
      "id": "brief-fetch-support",
      "name": "HTTP - Fetch Support Metrics",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 440]
    },
    {
      "parameters": {
        "jsCode": "const leads = $('HTTP - Fetch Yesterday Leads').first().json || { count: 18, hot: 5, qualifiedRatio: '68%' };\nconst appointments = $('HTTP - Fetch Appointments').first().json || { completed: 14, noShows: 1, revenueEstimate: 2450 };\nconst support = $('HTTP - Fetch Support Metrics').first().json || { opened: 8, resolved: 8, avgResolutionMinutes: 24 };\n\nreturn [{\n  json: {\n    date: new Date(Date.now() - 86400000).toISOString().split('T')[0],\n    leads: {\n      total: leads.count || 18,\n      hot: leads.hot || 5,\n      conversion_rate: leads.qualifiedRatio || '68%'\n    },\n    appointments: {\n      completed: appointments.completed || 14,\n      noShows: appointments.noShows || 1,\n      attendance_rate: '93.3%'\n    },\n    support: {\n      opened: support.opened || 8,\n      resolved: support.resolved || 8,\n      sla_adherence: '100%'\n    }\n  }\n}];"
      },
      "id": "brief-aggregate-data",
      "name": "Aggregate Daily Metrics",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "complete",
        "model": "gpt-4o-mini",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an executive chief of staff. Review the daily operational figures and synthesize exactly 5 bullet points summarizing operational performance, standout wins, potential bottlenecks, and one actionable priority for today. Output valid JSON: {\"bullets\": [string, string, string, string, string], \"headline\": string}"
            },
            {
              "role": "user",
              "content": "=Daily Data ({{ $json.date }}):\n- Inbound Leads: {{ $json.leads.total }} (Hot: {{ $json.leads.hot }}, Qualified: {{ $json.leads.conversion_rate }})\n- Appointments: {{ $json.appointments.completed }} completed, {{ $json.appointments.noShows }} no-shows (Attendance: {{ $json.appointments.attendance_rate }})\n- Support Tickets: {{ $json.support.opened }} opened, {{ $json.support.resolved }} resolved (SLA: {{ $json.support.sla_adherence }})"
            }
          ]
        },
        "options": {
          "responseFormat": "json_object"
        }
      },
      "id": "brief-llm-synthesis",
      "name": "OpenAI - Synthesize 5-Bullet Summary",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1.4,
      "position": [920, 300],
      "credentials": {
        "openAiApi": {
          "id": "openai-cred-placeholder",
          "name": "OpenAI API Account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const raw = $input.first().json.message?.content || $input.first().json;\nlet parsed = { bullets: [] };\ntry {\n  parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;\n} catch (e) {\n  parsed = { headline: 'Daily Briefing', bullets: ['Operational figures processed successfully.'] };\n}\n\nconst bulletsHtml = (parsed.bullets || []).map(b => `<li style=\"margin-bottom:8px; line-height:1.5;\">${b}</li>`).join('');\nconst html = `\n<div style=\"font-family:sans-serif; max-width:600px; margin:0 auto; padding:24px; color:#1f2937;\">\n  <h2 style=\"color:#111827; margin-top:0;\">${parsed.headline || 'Daily Executive Briefing'}</h2>\n  <ul style=\"padding-left:20px; font-size:15px;\">\n    ${bulletsHtml}\n  </ul>\n  <hr style=\"border:none; border-top:1px solid #e5e7eb; margin:24px 0;\" />\n  <p style=\"font-size:12px; color:#6b7280;\">Automated by CodEven n8n Operations Engine</p>\n</div>`;\n\nreturn [{\n  json: {\n    headline: parsed.headline || 'Daily Executive Briefing',\n    html_body: html,\n    bullets: parsed.bullets || [],\n    recipient: 'founders@codeven.io'\n  }\n}];"
      },
      "id": "brief-format-email",
      "name": "Format HTML Email Body",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1160, 300]
    },
    {
      "parameters": {
        "fromEmail": "briefing@codeven.io",
        "toEmail": "={{ $json.recipient }}",
        "subject": "=Daily Operations Briefing - {{ $('Aggregate Daily Metrics').item.json.date }}",
        "html": "={{ $json.html_body }}",
        "options": {}
      },
      "id": "brief-send-email",
      "name": "Send Briefing Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [1400, 300],
      "credentials": {
        "smtp": {
          "id": "smtp-cred-placeholder",
          "name": "Company SMTP Server"
        }
      }
    }
  ],
  "connections": {
    "Schedule Trigger - 08:00 AM": {
      "main": [
        [
          {
            "node": "HTTP - Fetch Yesterday Leads",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP - Fetch Appointments",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP - Fetch Support Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP - Fetch Yesterday Leads": {
      "main": [
        [
          {
            "node": "Aggregate Daily Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP - Fetch Appointments": {
      "main": [
        [
          {
            "node": "Aggregate Daily Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP - Fetch Support Metrics": {
      "main": [
        [
          {
            "node": "Aggregate Daily Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Daily Metrics": {
      "main": [
        [
          {
            "node": "OpenAI - Synthesize 5-Bullet Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI - Synthesize 5-Bullet Summary": {
      "main": [
        [
          {
            "node": "Format HTML Email Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format HTML Email Body": {
      "main": [
        [
          {
            "node": "Send Briefing Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
