{
  "name": "Get Call Stats \u2014 Sub-workflow",
  "nodes": [
    {
      "parameters": {
        "inputSource": "workflowInputs",
        "workflowInputs": {
          "values": [
            {
              "name": "startDate",
              "type": "string"
            },
            {
              "name": "endDate",
              "type": "string"
            }
          ]
        }
      },
      "id": "execute-workflow-trigger",
      "name": "Execute Workflow Trigger",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const input = $input.first().json;\nconst startDate = input.startDate || '2026-01-01';\nconst endDate = input.endDate || '2026-12-31';\n\nreturn [{\n  json: {\n    startDate,\n    endDate\n  }\n}];"
      },
      "id": "parse-params",
      "name": "Parse Parameters",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "<YOUR_GOOGLE_SHEETS_URL>"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0438"
        },
        "options": {
          "returnAllMatches": true
        }
      },
      "id": "get-all-results",
      "name": "Get All Call Results",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        440,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all().map(i => i.json);\nconst startDate = $('Parse Parameters').first().json.startDate;\nconst endDate = $('Parse Parameters').first().json.endDate;\n\n// Filter by date range\nconst filtered = items.filter(item => {\n  const d = item.Date;\n  if (!d) return false;\n  return d >= startDate && d <= endDate;\n});\n\nconst totalCalls = filtered.length;\nconst negativeCalls = filtered.filter(item => \n  item.Tonality && item.Tonality.toLowerCase() === 'negative'\n).length;\nconst negativeCallPercent = totalCalls > 0 \n  ? Math.round((negativeCalls / totalCalls) * 100) + '%' \n  : '0%';\n\nreturn [{\n  json: {\n    totalCalls,\n    negativeCalls,\n    negativeCallPercent\n  }\n}];"
      },
      "id": "calculate-stats",
      "name": "Calculate Stats",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    }
  ],
  "connections": {
    "Execute Workflow Trigger": {
      "main": [
        [
          {
            "node": "Parse Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Parameters": {
      "main": [
        [
          {
            "node": "Get All Call Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get All Call Results": {
      "main": [
        [
          {
            "node": "Calculate Stats",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false
  }
}