{
  "name": "ChurnRadar B2B Alert Workflow",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 * * 1-5"
            }
          ]
        }
      },
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://localhost:8000/predict",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "customer_id",
              "value": "CUST_DEMO_001"
            },
            {
              "name": "total_subs",
              "value": "3"
            },
            {
              "name": "avg_mobile_revenue",
              "value": "50.0"
            },
            {
              "name": "avg_fix_revenue",
              "value": "20.0"
            },
            {
              "name": "total_revenue",
              "value": "70.0"
            },
            {
              "name": "arpu",
              "value": "23.3"
            },
            {
              "name": "active_subscribers",
              "value": "2"
            },
            {
              "name": "not_active_subscribers",
              "value": "1"
            },
            {
              "name": "crm_segment",
              "value": "VIP"
            },
            {
              "name": "effective_segment",
              "value": "Business"
            }
          ]
        },
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.churn_prediction }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF Churn Prediction",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "messages": {
          "message": [
            {
              "role": "system",
              "content": "\ub2f9\uc2e0\uc740 B2B \ud1b5\uc2e0\uc0ac \uc774\ud0c8 \ubc29\uc5b4 \uc804\ubb38\uac00\uc785\ub2c8\ub2e4. \ub370\uc774\ud130(\uace0\uac1dID, \ud655\ub960, \uc704\ud5d8\ub3c4, \uc190\uc2e4\uc561)\ub97c \ubcf4\uace0 \uc601\uc5c5 \ub2f4\ub2f9\uc790\uac00 \uc989\uac01 \uc870\uce58\ud560 \uc218 \uc788\ub294 \uc9e7\uc740 \ub300\uc751 \uc2dc\ub098\ub9ac\uc624\ub97c \uc791\uc131\ud574\uc8fc\uc138\uc694."
            },
            {
              "role": "user",
              "content": "=\uace0\uac1dID: {{ $json.customer_id }}\n\uc774\ud0c8 \ud655\ub960: {{ $json.churn_probability }}\n\uc704\ud5d8\ub3c4: {{ $json.risk_level }}\n\uc608\uc0c1 \uc190\uc2e4\uc561: {{ $json.expected_revenue_loss }} BGN"
            }
          ]
        }
      },
      "name": "OpenAI Chat",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [
        600,
        280
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $('HTTP Request').item.json.risk_level }}",
              "value2": "HIGH"
            }
          ]
        }
      },
      "name": "IF Risk Level HIGH",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        800,
        280
      ]
    },
    {
      "parameters": {
        "channel": "#churn-alert-high",
        "text": "=\ud83d\udea8 [HIGH RISK] \uc774\ud0c8 \uacbd\ubcf4\n\n\uace0\uac1d ID: {{ $('HTTP Request').item.json.customer_id }}\n\uc774\ud0c8 \ud655\ub960: {{ $('HTTP Request').item.json.churn_probability }}\n\uc608\uc0c1 \uc190\uc2e4\uc561: {{ $('HTTP Request').item.json.expected_revenue_loss }} BGN\n\n\ud83d\udca1 GPT \ub300\uc751 \uc2dc\ub098\ub9ac\uc624:\n{{ $json.choices[0].message.content }}"
      },
      "name": "Slack (HIGH)",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1000,
        180
      ]
    },
    {
      "parameters": {
        "channel": "#churn-alert",
        "text": "=\u26a0\ufe0f [MEDIUM/LOW RISK] \uc774\ud0c8 \uacbd\ubcf4\n\n\uace0\uac1d ID: {{ $('HTTP Request').item.json.customer_id }}\n\uc774\ud0c8 \ud655\ub960: {{ $('HTTP Request').item.json.churn_probability }}\n\n\ud83d\udca1 GPT \ub300\uc751 \uc2dc\ub098\ub9ac\uc624:\n{{ $json.choices[0].message.content }}"
      },
      "name": "Slack (FALSE)",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1000,
        380
      ]
    },
    {
      "parameters": {
        "sendTo": "sales@baza-telecom.com",
        "subject": "=\ud83d\udea8 [\uae34\uae09] \ud575\uc2ec \uace0\uac1d \uc774\ud0c8 \uacbd\ubcf4 ({{ $('HTTP Request').item.json.customer_id }})",
        "message": "=\ub2f4\ub2f9\uc790\ub2d8,\n\n\uc544\ub798 \uace0\uac1d\uc758 \uc774\ud0c8 \uc704\ud5d8\uc774 \ub9e4\uc6b0 \ub192\uc2b5\ub2c8\ub2e4. \uc989\uac01\uc801\uc778 \uac1c\uc785\uc774 \ud544\uc694\ud569\ub2c8\ub2e4.\n\n\uace0\uac1d ID: {{ $('HTTP Request').item.json.customer_id }}\n\uc774\ud0c8 \ud655\ub960: {{ $('HTTP Request').item.json.churn_probability }}\n\uc608\uc0c1 \ub9e4\ucd9c \uc190\uc2e4: {{ $('HTTP Request').item.json.expected_revenue_loss }} BGN\n\n[\uc804\ubb38\uac00 \uc81c\uc5b8]\n{{ $('OpenAI Chat').item.json.choices[0].message.content }}\n\n\uc870\uce58 \ud6c4 Google Sheets\uc5d0 \uacb0\uacfc\ub97c \uae30\ub85d\ud574\uc8fc\uc138\uc694."
      },
      "name": "Gmail (HIGH)",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        1200,
        80
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "INSERT_GOOGLE_SHEET_ID_HERE",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "churn_log",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "timestamp": "={{ $now }}",
            "customer_id": "={{ $('HTTP Request').item.json.customer_id }}",
            "churn_probability": "={{ $('HTTP Request').item.json.churn_probability }}",
            "risk_level": "={{ $('HTTP Request').item.json.risk_level }}",
            "expected_revenue_loss": "={{ $('HTTP Request').item.json.expected_revenue_loss }}",
            "gpt_message": "={{ $('OpenAI Chat').item.json.choices[0].message.content }}",
            "action_taken": "",
            "result": ""
          }
        }
      },
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        1200,
        280
      ]
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "IF Churn Prediction",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Churn Prediction": {
      "main": [
        [
          {
            "node": "OpenAI Chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat": {
      "main": [
        [
          {
            "node": "IF Risk Level HIGH",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Risk Level HIGH": {
      "main": [
        [
          {
            "node": "Slack (HIGH)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack (FALSE)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slack (HIGH)": {
      "main": [
        [
          {
            "node": "Gmail (HIGH)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}