{
  "name": "System Health Monitor",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": [
          "00:00",
          "06:00",
          "12:00",
          "18:00"
        ],
        "timezone": "UTC"
      },
      "id": "node_schedule_trigger",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        100,
        100
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://titan-os-core-3xyn.vercel.app/api/health"
      },
      "id": "node_check_frontend",
      "name": "Check Frontend Health",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        300,
        50
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://titan-os-core-backend.onrender.com/health"
      },
      "id": "node_check_backend",
      "name": "Check Backend Health",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        300,
        150
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://maqqjchekovcgtbzrbrv.supabase.co/rest/v1/"
      },
      "id": "node_check_supabase",
      "name": "Check Supabase Connectivity",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        300,
        250
      ]
    },
    {
      "parameters": {
        "functionCode": "// Classify overall health status\nconst frontend = $('node_check_frontend').first().body;\nconst backend = $('node_check_backend').first().body;\nconst supabase = $('node_check_supabase').first();\n\nlet status = 'HEALTHY';\nlet issues = [];\nlet response_times = {};\n\nif (!frontend || frontend.status !== 'OK') { \n  status = 'DEGRADED';\n  issues.push('Frontend unreachable');\n}\nif (!backend || backend.status !== 'OK') { \n  status = 'DEGRADED';\n  issues.push('Backend unreachable');\n}\nif (!supabase || supabase.statusCode !== 200) { \n  status = 'CRITICAL';\n  issues.push('Supabase unreachable');\n}\n\nreturn [{\n  health_status: status,\n  issues: issues,\n  timestamp: new Date().toISOString(),\n  frontend_ok: frontend && frontend.status === 'OK',\n  backend_ok: backend && backend.status === 'OK',\n  supabase_ok: supabase && supabase.statusCode === 200\n}];"
      },
      "id": "node_classify_health",
      "name": "Classify Health Status",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        150
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://maqqjchekovcgtbzrbrv.supabase.co/rest/v1/deployment_monitor",
        "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": "service_name",
              "value": "TITAN_OS_CORE"
            },
            {
              "name": "health_status",
              "value": "{{ $input.first().health_status }}"
            },
            {
              "name": "frontend_status",
              "value": "{{ $input.first().frontend_ok ? 'OK' : 'FAILED' }}"
            },
            {
              "name": "backend_status",
              "value": "{{ $input.first().backend_ok ? 'OK' : 'FAILED' }}"
            },
            {
              "name": "database_status",
              "value": "{{ $input.first().supabase_ok ? 'OK' : 'FAILED' }}"
            },
            {
              "name": "issues",
              "value": "={{ JSON.stringify($input.first().issues) }}"
            },
            {
              "name": "check_timestamp",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        }
      },
      "id": "node_log_deployment",
      "name": "Log to Deployment Monitor",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        700,
        150
      ]
    },
    {
      "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": "SYSTEM_ALERT"
            },
            {
              "name": "assigned_to",
              "value": "OPS_TEAM"
            },
            {
              "name": "priority",
              "value": "{{ $input.first().health_status === 'CRITICAL' ? 'HIGH' : 'MEDIUM' }}"
            },
            {
              "name": "status",
              "value": "PENDING"
            },
            {
              "name": "description",
              "value": "System health check: {{ $input.first().health_status }} - Issues: {{ $input.first().issues.join(', ') }}"
            }
          ]
        }
      },
      "id": "node_create_alert_task",
      "name": "Create Alert Task (if issues)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        900,
        250
      ]
    },
    {
      "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": "System Health Monitor"
            },
            {
              "name": "workflow_run_id",
              "value": "={{ $execution.id }}"
            },
            {
              "name": "status",
              "value": "COMPLETED"
            },
            {
              "name": "action_log",
              "value": "Health check executed. Status: {{ $input.first().health_status }}. Issues: {{ $input.first().issues.join('; ') || 'None' }}"
            },
            {
              "name": "execution_timestamp",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        }
      },
      "id": "node_audit_log",
      "name": "Write Audit Log",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1100,
        150
      ]
    }
  ],
  "connections": {
    "node_schedule_trigger": {
      "main": [
        [
          {
            "node": "node_check_frontend",
            "branch": 0
          },
          {
            "node": "node_check_backend",
            "branch": 0
          },
          {
            "node": "node_check_supabase",
            "branch": 0
          }
        ]
      ]
    },
    "node_check_frontend": {
      "main": [
        [
          {
            "node": "node_classify_health",
            "branch": 0
          }
        ]
      ]
    },
    "node_check_backend": {
      "main": [
        [
          {
            "node": "node_classify_health",
            "branch": 0
          }
        ]
      ]
    },
    "node_check_supabase": {
      "main": [
        [
          {
            "node": "node_classify_health",
            "branch": 0
          }
        ]
      ]
    },
    "node_classify_health": {
      "main": [
        [
          {
            "node": "node_log_deployment",
            "branch": 0
          }
        ]
      ]
    },
    "node_log_deployment": {
      "main": [
        [
          {
            "node": "node_create_alert_task",
            "branch": 0
          }
        ]
      ]
    },
    "node_create_alert_task": {
      "main": [
        [
          {
            "node": "node_audit_log",
            "branch": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}