{
  "name": "Bet Assistant - Monitoring i Alerty Systemu",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/15 * * * *"
            }
          ]
        }
      },
      "name": "Cron: Co 15 minut",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "id": "cron-every-15min",
      "notes": "Regularny health check systemu"
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "apiKey",
              "value": "={{ $env.BET_ASSISTANT_WEBHOOK_KEY }}"
            }
          ]
        }
      },
      "name": "Ustaw API key",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        300
      ],
      "id": "set-api-key"
    },
    {
      "parameters": {
        "url": "={{ $env.BET_ASSISTANT_API_URL }}/api/webhooks/n8n/health",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-n8n-api-key",
              "value": "={{ $json.apiKey }}"
            }
          ]
        },
        "options": {
          "timeout": 10000
        }
      },
      "name": "HTTP - Health Check",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        680,
        300
      ],
      "id": "http-health-check",
      "notes": "Sprawdza czy API dzia\u0142a"
    },
    {
      "parameters": {
        "url": "={{ $env.BET_ASSISTANT_API_URL }}/api/webhooks/n8n/status",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-n8n-api-key",
              "value": "={{ $json.apiKey }}"
            }
          ]
        },
        "options": {
          "timeout": 10000
        }
      },
      "name": "HTTP - Get System Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        680,
        100
      ],
      "id": "http-system-status",
      "notes": "Pobiera statystyki systemu"
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $node['HTTP - Health Check'].json.success }}",
              "value2": true
            }
          ]
        }
      },
      "name": "Czy system dzia\u0142a?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        900,
        300
      ],
      "id": "check-system-health"
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $node['HTTP - Get System Status'].json.stats.totalMatches }}",
              "operation": "smaller",
              "value2": 100
            }
          ]
        }
      },
      "name": "Czy niska liczba mecz\u00f3w?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1120,
        100
      ],
      "id": "check-low-matches",
      "notes": "Alert je\u015bli < 100 mecz\u00f3w w bazie (co\u015b posz\u0142o nie tak)"
    },
    {
      "parameters": {
        "resource": "message",
        "text": "\u26a0\ufe0f SYSTEM ALERT: Niska liczba mecz\u00f3w!\n\n\ud83d\udcca Statystyki:\n\u2022 Mecze w bazie: {{ $node['HTTP - Get System Status'].json.stats.totalMatches }}\n\u2022 W\u0142\u0105czone ligi: {{ $node['HTTP - Get System Status'].json.stats.enabledLeagues }}\n\n\ud83d\udd0d Mo\u017cliwe przyczyny:\n- B\u0142\u0105d importu\n- Baza danych zosta\u0142a wyczyszczona\n- Problem z API Football\n\n\u2705 Sprawd\u017a logi i status import\u00f3w!",
        "additionalFields": {}
      },
      "name": "Slack - Alert: Niska liczba mecz\u00f3w",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [
        1340,
        100
      ],
      "id": "slack-low-matches-alert",
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "text": "\ud83d\udea8 SYSTEM DOWN!\n\nBet Assistant API nie odpowiada!\n\n\ud83d\udd50 Timestamp: {{ $now.toISO() }}\n\n\u274c Health check error:\n{{ $json.error || 'No response from server' }}\n\n\u26a0\ufe0f NATYCHMIASTOWE DZIA\u0141ANIE WYMAGANE!",
        "additionalFields": {}
      },
      "name": "Slack - KRYTYCZNY: System Down",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [
        1120,
        400
      ],
      "id": "slack-system-down",
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "notes": "Alert krytyczny - system nie dzia\u0142a"
    },
    {
      "parameters": {
        "operation": "sendEmail",
        "fromEmail": "={{ $env.SMTP_USER }}",
        "toEmail": "={{ $env.NOTIFICATION_EMAIL }}",
        "subject": "\ud83d\udea8 KRYTYCZNY: Bet Assistant System Down!",
        "emailFormat": "html",
        "text": "<h1 style=\"color: red;\">\ud83d\udea8 SYSTEM DOWN!</h1>\n\n<p>Bet Assistant API nie odpowiada!</p>\n\n<h3>Szczeg\u00f3\u0142y:</h3>\n<ul>\n<li>Timestamp: <b>{{ $now.toISO() }}</b></li>\n<li>Health check: <b>FAILED</b></li>\n</ul>\n\n<h3>B\u0142\u0105d:</h3>\n<pre>{{ JSON.stringify($json, null, 2) }}</pre>\n\n<p><b>NATYCHMIASTOWE DZIA\u0141ANIE WYMAGANE!</b></p>",
        "options": {}
      },
      "name": "Email - KRYTYCZNY Alert",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1120,
        500
      ],
      "id": "email-critical-alert",
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "notes": "Email w przypadku krytycznego b\u0142\u0119du"
    },
    {
      "parameters": {
        "mode": "raw",
        "jsonOutput": "={{ {\n  timestamp: $now.toISO(),\n  type: 'health_check',\n  healthy: $node['HTTP - Health Check'].json.success,\n  totalMatches: $node['HTTP - Get System Status'].json.stats?.totalMatches,\n  enabledLeagues: $node['HTTP - Get System Status'].json.stats?.enabledLeagues,\n  recentImports: $node['HTTP - Get System Status'].json.stats?.recentImports?.length\n} }}",
        "options": {}
      },
      "name": "Loguj monitoring",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        1120,
        200
      ],
      "id": "log-monitoring-data",
      "disabled": true,
      "notes": "Opcjonalne: zapisz do systemu logowania/metryki"
    }
  ],
  "connections": {
    "Cron: Co 15 minut": {
      "main": [
        [
          {
            "node": "Ustaw API key",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ustaw API key": {
      "main": [
        [
          {
            "node": "HTTP - Health Check",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP - Get System Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP - Health Check": {
      "main": [
        [
          {
            "node": "Czy system dzia\u0142a?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP - Get System Status": {
      "main": [
        [
          {
            "node": "Czy niska liczba mecz\u00f3w?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Czy system dzia\u0142a?": {
      "main": [
        [
          {
            "node": "Loguj monitoring",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack - KRYTYCZNY: System Down",
            "type": "main",
            "index": 0
          },
          {
            "node": "Email - KRYTYCZNY Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Czy niska liczba mecz\u00f3w?": {
      "main": [
        [
          {
            "node": "Slack - Alert: Niska liczba mecz\u00f3w",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "Bet Assistant",
      "id": "1"
    },
    {
      "name": "Monitoring",
      "id": "6"
    },
    {
      "name": "Critical",
      "id": "5"
    }
  ]
}