{
  "name": "PRAGMAS - Main Analysis",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "pragmas/main-analysis",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "1",
      "name": "Main Analysis Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $json.body ?? {};\nconst query = $json.query ?? {};\nconst company = String(body.company ?? query.company ?? $json.company ?? '').trim();\nif (!company) {\n  throw new Error('company is required');\n}\nconst reportDate = String(body.reportDate ?? query.reportDate ?? new Date().toISOString().slice(0, 10)).trim();\nconst queryText = String(body.query ?? query.query ?? `${company} unit economics retention margin`).trim();\nconst shellQuote = (value) => JSON.stringify(String(value)).replace(/[`$]/g, '\\\\$&');\nconst buildCommand = (script, args) => [script, ...args].map((part, index) => index === 0 ? part : shellQuote(part)).join(' ');\nreturn [{\n  json: {\n    company,\n    reportDate,\n    query: queryText,\n    analysisCommand: buildCommand('python3 /workspace/scripts/analysis/run_analysis.py', ['--vault-path', '/workspace/obsidian-vault', '--company', company, '--report-date', reportDate]),\n    dashboardCommand: buildCommand('python3 /workspace/scripts/dashboard/generate_dashboard.py', ['--vault-path', '/workspace/obsidian-vault', '--company', company]),\n    pdfCommand: buildCommand('python3 /workspace/scripts/dashboard/generate_latex_pdf.py', ['--vault-path', '/workspace/obsidian-vault', '--company', company])\n  }\n}];"
      },
      "id": "2",
      "name": "Prepare Request",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$env.OBSIDIAN_API_BASE_URL + '/agent/context'}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{'Bearer ' + $env.OBSIDIAN_API_TOKEN}}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "company",
              "value": "={{$json.company}}"
            },
            {
              "name": "query",
              "value": "={{$json.query}}"
            },
            {
              "name": "limit",
              "value": "6"
            }
          ]
        },
        "options": {}
      },
      "id": "3",
      "name": "Pull Vault Context",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        760,
        300
      ]
    },
    {
      "parameters": {
        "command": "={{$('Prepare Request').item.json.analysisCommand}}"
      },
      "id": "4",
      "name": "Run Analysis Script",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1020,
        300
      ]
    },
    {
      "parameters": {
        "command": "={{$('Prepare Request').item.json.dashboardCommand}}"
      },
      "id": "5",
      "name": "Build Dashboard",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1280,
        300
      ]
    },
    {
      "parameters": {
        "command": "={{$('Prepare Request').item.json.pdfCommand}}"
      },
      "id": "6",
      "name": "Build PDF",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1540,
        300
      ]
    }
  ],
  "connections": {
    "Main Analysis Webhook": {
      "main": [
        [
          {
            "node": "Prepare Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Request": {
      "main": [
        [
          {
            "node": "Pull Vault Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pull Vault Context": {
      "main": [
        [
          {
            "node": "Run Analysis Script",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Run Analysis Script": {
      "main": [
        [
          {
            "node": "Build Dashboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Dashboard": {
      "main": [
        [
          {
            "node": "Build PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "pragmas-main-analysis-v2",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "pragmas-main-analysis"
}