{
  "name": "AI-Powered Daily Briefing V2 (HTTP)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * *"
            }
          ]
        }
      },
      "id": "daily-trigger",
      "name": "Daily Trigger (8 AM Pacific)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "// Get date for queries\nconst today = new Date().toISOString().split('T')[0];\nconst weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];\nconst weekFromNow = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];\n\nreturn [{ \n  today,\n  week_ago: weekAgo,\n  week_from_now: weekFromNow\n}];"
      },
      "id": "prepare-dates",
      "name": "Prepare Dates",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        400
      ]
    },
    {
      "parameters": {
        "url": "https://YOUR-PROJECT-REF.supabase.co/functions/v1/second-brain-mcp",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\n  jsonrpc: '2.0',\n  id: 1,\n  method: 'tools/call',\n  params: {\n    name: 'list-tasks',\n    arguments: {\n      status: 'pending',\n      due_date_to: $json.week_from_now,\n      limit: 50,\n      sort_by: 'due_date',\n      sort_order: 'asc'\n    }\n  }\n}) }}",
        "options": {}
      },
      "id": "get-tasks",
      "name": "Get Pending Tasks (MCP)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "https://YOUR-PROJECT-REF.supabase.co/functions/v1/second-brain-mcp",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\n  jsonrpc: '2.0',\n  id: 2,\n  method: 'tools/call',\n  params: {\n    name: 'list-contacts',\n    arguments: {\n      contact_status: 'to reach out',\n      limit: 20,\n      format: 'summary'\n    }\n  }\n}) }}",
        "options": {}
      },
      "id": "get-followups",
      "name": "Get Follow-ups (MCP)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        400
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "https://YOUR-PROJECT-REF.supabase.co/functions/v1/second-brain-mcp",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\n  jsonrpc: '2.0',\n  id: 3,\n  method: 'tools/call',\n  params: {\n    name: 'list-opportunities',\n    arguments: {\n      status: '0_research,1_to_apply',\n      limit: 10,\n      sort_by: 'weighted_score',\n      sort_order: 'desc',\n      format: 'summary'\n    }\n  }\n}) }}",
        "options": {}
      },
      "id": "get-opportunities",
      "name": "Get Top Opportunities (MCP)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        500
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Collect data from all parallel queries\nconst tasks = $('Get Pending Tasks (MCP)').item.json.result?.content?.[0]?.text || '{}';\nconst followups = $('Get Follow-ups (MCP)').item.json.result?.content?.[0]?.text || '{}';\nconst opportunities = $('Get Top Opportunities (MCP)').item.json.result?.content?.[0]?.text || '{}';\nconst dates = $('Prepare Dates').item.json;\n\n// Parse if strings\nlet parsedTasks, parsedFollowups, parsedOpportunities;\ntry {\n  parsedTasks = typeof tasks === 'string' ? JSON.parse(tasks) : tasks;\n  parsedFollowups = typeof followups === 'string' ? JSON.parse(followups) : followups;\n  parsedOpportunities = typeof opportunities === 'string' ? JSON.parse(opportunities) : opportunities;\n} catch (e) {\n  parsedTasks = { items: [] };\n  parsedFollowups = { items: [] };\n  parsedOpportunities = { items: [] };\n}\n\nreturn [{\n  tasks: parsedTasks.items || parsedTasks,\n  followups: parsedFollowups.items || parsedFollowups,\n  opportunities: parsedOpportunities.items || parsedOpportunities,\n  today: dates.today,\n  task_count: (parsedTasks.items || parsedTasks).length || 0,\n  followup_count: (parsedFollowups.items || parsedFollowups).length || 0,\n  opportunity_count: (parsedOpportunities.items || parsedOpportunities).length || 0\n}];"
      },
      "id": "aggregate-data",
      "name": "Aggregate Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        400
      ]
    },
    {
      "parameters": {
        "url": "https://api.anthropic.com/v1/messages",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "anthropicApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "anthropic-version",
              "value": "2023-06-01"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\n  model: 'claude-sonnet-4-20250514',\n  max_tokens: 2048,\n  temperature: 0.5,\n  messages: [{\n    role: 'user',\n    content: `Generate a personalized daily briefing for ${$json.today}.\n\nDATA FROM SECOND BRAIN:\n\nPENDING TASKS (${$json.task_count}):\n${JSON.stringify($json.tasks, null, 2)}\n\nFOLLOW-UPS NEEDED (${$json.followup_count}):\n${JSON.stringify($json.followups, null, 2)}\n\nTOP OPPORTUNITIES (${$json.opportunity_count}):\n${JSON.stringify($json.opportunities, null, 2)}\n\nGENERATE BRIEFING:\n\n1. **Today's Priority** (1-2 sentences)\n   - ONE most important focus for today\n   - Why it matters\n\n2. **Critical Tasks** (Top 5)\n   - List by intelligent priority (urgency + importance + impact)\n   - Not just by due date\n   - Include context for each\n\n3. **Key Follow-ups** (Top 3)\n   - Who to reach out to\n   - Why (context from past interactions)\n   - Suggested talking points\n\n4. **Opportunities to Watch** (Top 2-3)\n   - Sorted by weighted_score\n   - Deadlines approaching\n   - Action items\n\n5. **Quick Wins** (<15 min tasks)\n   - 2-3 easy wins for momentum\n\n6. **Relationship Insights**\n   - Patterns in follow-ups\n   - Network gaps\n   - Connection opportunities\n\nTONE: Concise, actionable, motivating\nFORMAT: Well-formatted markdown (not HTML)\nLENGTH: ~400-600 words\n\nReturn ONLY the briefing content (no JSON wrapper).`\n  }]\n}) }}",
        "options": {}
      },
      "id": "generate-briefing",
      "name": "Generate Briefing (Claude API)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        400
      ],
      "credentials": {
        "anthropicApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const response = $input.item.json;\nconst briefingContent = response.content?.[0]?.text || 'Failed to generate briefing';\nconst aggregateData = $('Aggregate Data').item.json;\n\nreturn [{\n  subject: `Daily Briefing - ${aggregateData.today}`,\n  briefing: briefingContent,\n  stats: {\n    tasks: aggregateData.task_count,\n    followups: aggregateData.followup_count,\n    opportunities: aggregateData.opportunity_count\n  },\n  timestamp: new Date().toISOString()\n}];"
      },
      "id": "format-email",
      "name": "Format Email",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1340,
        400
      ]
    },
    {
      "parameters": {
        "fromEmail": "your-email@example.com",
        "toEmail": "your-email@example.com",
        "subject": "={{ $json.subject }}",
        "emailFormat": "text",
        "text": "={{ `\ud83d\udcca SECOND BRAIN DAILY BRIEFING\\n${'='.repeat(50)}\\n\\nTasks: ${$json.stats.tasks} | Follow-ups: ${$json.stats.followups} | Opportunities: ${$json.stats.opportunities}\\n\\n${$json.briefing}\\n\\n${'='.repeat(50)}\\n\ud83e\udd16 Generated by Second Brain at ${$json.timestamp}` }}",
        "options": {}
      },
      "id": "send-email",
      "name": "Send Email Briefing",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1560,
        400
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Daily Trigger (8 AM Pacific)": {
      "main": [
        [
          {
            "node": "Prepare Dates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Dates": {
      "main": [
        [
          {
            "node": "Get Pending Tasks (MCP)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Follow-ups (MCP)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Top Opportunities (MCP)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Pending Tasks (MCP)": {
      "main": [
        [
          {
            "node": "Aggregate Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Follow-ups (MCP)": {
      "main": [
        [
          {
            "node": "Aggregate Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Top Opportunities (MCP)": {
      "main": [
        [
          {
            "node": "Aggregate Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Data": {
      "main": [
        [
          {
            "node": "Generate Briefing (Claude API)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Briefing (Claude API)": {
      "main": [
        [
          {
            "node": "Format Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Email": {
      "main": [
        [
          {
            "node": "Send Email Briefing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "createdAt": "2025-12-16T00:00:00.000Z",
      "updatedAt": "2025-12-16T00:00:00.000Z",
      "id": "automation",
      "name": "Automation"
    },
    {
      "createdAt": "2025-12-16T00:00:00.000Z",
      "updatedAt": "2025-12-16T00:00:00.000Z",
      "id": "daily",
      "name": "Daily"
    }
  ],
  "triggerCount": 1,
  "updatedAt": "2025-12-16T00:00:00.000Z",
  "versionId": "2"
}