{
  "name": "Founder Approval Gate",
  "nodes": [
    {
      "parameters": {
        "path": "webhook/founder-approval",
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "id": "node_webhook_trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        100,
        100
      ]
    },
    {
      "parameters": {
        "functionCode": "// Validate founder approval decision\nconst decision = $input.first().body;\n\nconst valid_decisions = ['APPROVE', 'REJECT', 'HOLD', 'MODIFY'];\nconst is_valid = valid_decisions.includes(decision.decision);\n\nif (!is_valid) {\n  return [{ error: 'Invalid decision type', decision: decision.decision }];\n}\n\nreturn [{\n  ...decision,\n  processed_at: new Date().toISOString(),\n  founder_id: decision.founder_id || 'FOUNDER',\n  approval_id: decision.approval_id\n}];"
      },
      "id": "node_validate_decision",
      "name": "Validate Decision",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        300,
        100
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "https://maqqjchekovcgtbzrbrv.supabase.co/rest/v1/approval_requests?id=eq.{{ $input.first().approval_id }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SUPABASE_SERVICE_ROLE_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "requestFormat": "json",
        "jsonParameters": {
          "parameters": [
            {
              "name": "status",
              "value": "{{ $input.first().decision === 'APPROVE' ? 'APPROVED' : $input.first().decision === 'REJECT' ? 'REJECTED' : 'HELD' }}"
            },
            {
              "name": "founder_decision",
              "value": "{{ $input.first().decision }}"
            },
            {
              "name": "founder_notes",
              "value": "{{ $input.first().notes || '' }}"
            },
            {
              "name": "decided_at",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        }
      },
      "id": "node_update_approval",
      "name": "Update Approval Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        500,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://maqqjchekovcgtbzrbrv.supabase.co/rest/v1/task_queue",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SUPABASE_SERVICE_ROLE_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "requestFormat": "json",
        "jsonParameters": {
          "parameters": [
            {
              "name": "task_type",
              "value": "{{ $input.first().decision === 'APPROVE' ? 'EXECUTE_APPROVED_ACTION' : $input.first().decision === 'REJECT' ? 'NOTIFY_REJECTION' : 'RESUBMIT_HELD_ITEM' }}"
            },
            {
              "name": "assigned_to",
              "value": "{{ $input.first().decision === 'APPROVE' ? 'EXECUTION_TEAM' : 'PA_TEAM' }}"
            },
            {
              "name": "status",
              "value": "PENDING"
            },
            {
              "name": "priority",
              "value": "{{ $input.first().decision === 'APPROVE' ? 'HIGH' : 'MEDIUM' }}"
            },
            {
              "name": "reference",
              "value": "{{ $input.first().approval_id }}"
            },
            {
              "name": "description",
              "value": "Founder decision: {{ $input.first().decision }}. Notes: {{ $input.first().notes || 'None' }}"
            }
          ]
        }
      },
      "id": "node_create_task",
      "name": "Create Execution Task",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        700,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://maqqjchekovcgtbzrbrv.supabase.co/rest/v1/execution_logs",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SUPABASE_SERVICE_ROLE_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "requestFormat": "json",
        "jsonParameters": {
          "parameters": [
            {
              "name": "workflow_name",
              "value": "Founder Approval Gate"
            },
            {
              "name": "workflow_run_id",
              "value": "={{ $execution.id }}"
            },
            {
              "name": "status",
              "value": "COMPLETED"
            },
            {
              "name": "founder_action",
              "value": "{{ $input.first().decision }}"
            },
            {
              "name": "action_log",
              "value": "Founder decision recorded: {{ $input.first().decision }} on approval_id={{ $input.first().approval_id }}. Notes: {{ $input.first().notes || 'None' }}"
            },
            {
              "name": "execution_timestamp",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        }
      },
      "id": "node_audit_log",
      "name": "Write Audit Log",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        900,
        100
      ]
    },
    {
      "parameters": {
        "responseData": "={{ { success: true, decision: $input.first().decision, approval_id: $input.first().approval_id, message: 'Approval decision processed' } }}"
      },
      "id": "node_respond_success",
      "name": "Respond - Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1100,
        100
      ]
    },
    {
      "parameters": {
        "fromEmail": "noreply@titancrestre.ae",
        "toEmail": "{{ $env.FOUNDER_EMAIL }}",
        "subject": "Approval Executed - {{ $input.first().approval_id }}",
        "emailType": "html",
        "htmlBody": "<p>Your approval decision ({{ $input.first().decision }}) has been recorded and assigned to the execution team.</p>"
      },
      "id": "node_email_disabled",
      "name": "Send Confirmation Email (DISABLED)",
      "type": "n8n-nodes-base.sendGrid",
      "typeVersion": 1,
      "position": [
        1100,
        250
      ],
      "disabled": true
    }
  ],
  "connections": {
    "node_webhook_trigger": {
      "main": [
        [
          {
            "node": "node_validate_decision",
            "branch": 0
          }
        ]
      ]
    },
    "node_validate_decision": {
      "main": [
        [
          {
            "node": "node_update_approval",
            "branch": 0
          }
        ]
      ]
    },
    "node_update_approval": {
      "main": [
        [
          {
            "node": "node_create_task",
            "branch": 0
          }
        ]
      ]
    },
    "node_create_task": {
      "main": [
        [
          {
            "node": "node_audit_log",
            "branch": 0
          }
        ]
      ]
    },
    "node_audit_log": {
      "main": [
        [
          {
            "node": "node_respond_success",
            "branch": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}