{
  "name": "Quantra AI Worker - Main Loop",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Every 5 Minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/rpc/get_pending_ai_tasks",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "options": {}
      },
      "id": "get-pending-tasks",
      "name": "Get Pending AI Tasks",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "check-tasks",
              "leftValue": "={{ $json.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "has-tasks",
      "name": "Has Tasks?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "split-tasks",
      "name": "Split Into Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        660,
        -100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is-time-off",
              "leftValue": "={{ $json.task_type }}",
              "rightValue": "time_off",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "route-time-off",
      "name": "Time Off?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        880,
        -200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is-expense",
              "leftValue": "={{ $json.task_type }}",
              "rightValue": "expense",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "route-expense",
      "name": "Expense?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        880,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is-shift-swap",
              "leftValue": "={{ $json.task_type }}",
              "rightValue": "shift_swap",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "route-shift-swap",
      "name": "Shift Swap?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        880,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "// AI Confidence Calculator for Time Off Requests\nconst request = $input.first().json;\n\nlet confidence = 50;\nconst reasoning = [];\n\n// Simple confidence calculation\nconfidence += 20; // Base approval tendency\nreasoning.push('Request received and validated');\n\nif (request.data) {\n  confidence += 10;\n  reasoning.push('Complete request data provided');\n}\n\nconfidence = Math.max(0, Math.min(100, confidence));\n\nlet action = 'escalate';\nif (confidence >= 85) {\n  action = 'auto_approve';\n} else if (confidence >= 50) {\n  action = 'suggest_approval';\n}\n\nreturn {\n  json: {\n    request_id: request.id,\n    request_type: 'time_off',\n    confidence,\n    reasoning,\n    action,\n    company_id: request.company_id,\n    employee_id: request.employee_id\n  }\n};"
      },
      "id": "calc-time-off-confidence",
      "name": "Calculate Time Off Confidence",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        -200
      ]
    },
    {
      "parameters": {
        "jsCode": "// AI Confidence Calculator for Expense Reports\nconst expense = $input.first().json;\n\nlet confidence = 50;\nconst reasoning = [];\n\nconfidence += 20;\nreasoning.push('Expense request received');\n\nif (expense.data) {\n  confidence += 10;\n  reasoning.push('Complete expense data provided');\n}\n\nconfidence = Math.max(0, Math.min(100, confidence));\n\nlet action = 'escalate';\nif (confidence >= 85) {\n  action = 'auto_approve';\n} else if (confidence >= 50) {\n  action = 'suggest_approval';\n}\n\nreturn {\n  json: {\n    request_id: expense.id,\n    request_type: 'expense',\n    confidence,\n    reasoning,\n    action,\n    company_id: expense.company_id,\n    employee_id: expense.employee_id\n  }\n};"
      },
      "id": "calc-expense-confidence",
      "name": "Calculate Expense Confidence",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// AI Confidence Calculator for Shift Swaps\nconst swap = $input.first().json;\n\nlet confidence = 50;\nconst reasoning = [];\n\nconfidence += 20;\nreasoning.push('Shift swap request received');\n\nif (swap.data) {\n  confidence += 10;\n  reasoning.push('Complete swap data provided');\n}\n\nconfidence = Math.max(0, Math.min(100, confidence));\n\nlet action = 'escalate';\nif (confidence >= 85) {\n  action = 'auto_approve';\n} else if (confidence >= 50) {\n  action = 'suggest_approval';\n}\n\nreturn {\n  json: {\n    request_id: swap.id,\n    request_type: 'shift_swap',\n    confidence,\n    reasoning,\n    action,\n    company_id: swap.company_id,\n    employee_id: swap.employee_id\n  }\n};"
      },
      "id": "calc-shift-swap-confidence",
      "name": "Calculate Shift Swap Confidence",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        200
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "mergeByFields": {
          "values": []
        },
        "options": {}
      },
      "id": "merge-decisions",
      "name": "Merge All Decisions",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        1320,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is-auto-approve",
              "leftValue": "={{ $json.action }}",
              "rightValue": "auto_approve",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "should-auto-approve",
      "name": "Auto Approve?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1540,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/rpc/ai_auto_approve",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"p_request_id\": \"{{ $json.request_id }}\",\n  \"p_request_type\": \"{{ $json.request_type }}\",\n  \"p_confidence\": {{ $json.confidence }},\n  \"p_reasoning\": {{ JSON.stringify($json.reasoning) }}\n}",
        "options": {}
      },
      "id": "execute-auto-approve",
      "name": "Execute Auto Approve",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1760,
        -100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/rpc/ai_suggest_approval",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"p_request_id\": \"{{ $json.request_id }}\",\n  \"p_request_type\": \"{{ $json.request_type }}\",\n  \"p_confidence\": {{ $json.confidence }},\n  \"p_reasoning\": {{ JSON.stringify($json.reasoning) }},\n  \"p_action\": \"{{ $json.action }}\"\n}",
        "options": {}
      },
      "id": "create-suggestion",
      "name": "Create Suggestion for Manager",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1760,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/quantra_actions",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Prefer",
              "value": "return=minimal"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"company_id\": \"{{ $json.company_id }}\",\n  \"action_type\": \"ai_{{ $json.action }}\",\n  \"action_category\": \"approval\",\n  \"target_type\": \"{{ $json.request_type }}\",\n  \"target_id\": \"{{ $json.request_id }}\",\n  \"decision\": \"{{ $json.action }}\",\n  \"confidence_score\": {{ $json.confidence }},\n  \"reasoning\": {{ JSON.stringify($json.reasoning) }},\n  \"status\": \"completed\",\n  \"executed_at\": \"{{ new Date().toISOString() }}\"\n}",
        "options": {}
      },
      "id": "log-activity",
      "name": "Log AI Activity",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1980,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "status",
              "name": "status",
              "value": "Notification would be sent here",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "send-notification",
      "name": "Send Notification (Placeholder)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2200,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "status",
              "name": "status",
              "value": "No pending tasks",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "no-tasks",
      "name": "No Tasks - Skip",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        660,
        100
      ]
    }
  ],
  "connections": {
    "Every 5 Minutes": {
      "main": [
        [
          {
            "node": "Get Pending AI Tasks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Pending AI Tasks": {
      "main": [
        [
          {
            "node": "Has Tasks?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Tasks?": {
      "main": [
        [
          {
            "node": "Split Into Items",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Tasks - Skip",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Into Items": {
      "main": [
        [
          {
            "node": "Time Off?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Expense?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Shift Swap?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Time Off?": {
      "main": [
        [
          {
            "node": "Calculate Time Off Confidence",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Expense?": {
      "main": [
        [
          {
            "node": "Calculate Expense Confidence",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Shift Swap?": {
      "main": [
        [
          {
            "node": "Calculate Shift Swap Confidence",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Calculate Time Off Confidence": {
      "main": [
        [
          {
            "node": "Merge All Decisions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Expense Confidence": {
      "main": [
        [
          {
            "node": "Merge All Decisions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Shift Swap Confidence": {
      "main": [
        [
          {
            "node": "Merge All Decisions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge All Decisions": {
      "main": [
        [
          {
            "node": "Auto Approve?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Auto Approve?": {
      "main": [
        [
          {
            "node": "Execute Auto Approve",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Suggestion for Manager",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Auto Approve": {
      "main": [
        [
          {
            "node": "Log AI Activity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Suggestion for Manager": {
      "main": [
        [
          {
            "node": "Log AI Activity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log AI Activity": {
      "main": [
        [
          {
            "node": "Send Notification (Placeholder)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "tags": [
    {
      "name": "quantra-core"
    },
    {
      "name": "ai-worker"
    }
  ],
  "versionId": "2"
}