#!/usr/bin/env bash
# =============================================================================
# Debezium connector 注册脚本
# 用法: ./scripts/debezium/register-connectors.sh [CONNECT_URL]
#
# 经验教训 (rwadurian 事故):
#   - 必须使用 debezium/connect:2.5.4.Final (修复 DBZ-7316 WAL 积压 bug)
#   - heartbeat 使用 TABLE 方式而非 pg_logical_emit_message
#   - Kafka Connect REST API 不能暴露公网 (SSRF 注入风险)
#   - 配置 signal channel 以便出问题时可用增量快照重放数据
# =============================================================================
set -euo pipefail

CONNECT_URL="${1:-http://localhost:8083}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "Waiting for Kafka Connect to be ready..."
until curl -sf "${CONNECT_URL}/" > /dev/null 2>&1; do
    echo "  Kafka Connect not ready, retrying in 5s..."
    sleep 5
done
echo "Kafka Connect is ready at ${CONNECT_URL}"

# 注册 outbox connector
CONNECTOR_NAME="genex-outbox-connector"
CONFIG_FILE="${SCRIPT_DIR}/outbox-connector.json"

if [ ! -f "$CONFIG_FILE" ]; then
    echo "ERROR: Config file not found: $CONFIG_FILE"
    exit 1
fi

# 检查 connector 是否已存在
STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" "${CONNECT_URL}/connectors/${CONNECTOR_NAME}")

if [ "$STATUS_CODE" = "200" ]; then
    echo "Connector '${CONNECTOR_NAME}' already exists. Updating config..."
    # 提取 config 部分进行 PUT 更新
    CONFIG_ONLY=$(python3 -c "import json,sys; d=json.load(open(sys.argv[1])); print(json.dumps(d['config']))" "$CONFIG_FILE" 2>/dev/null \
        || python -c "import json,sys; d=json.load(open(sys.argv[1])); print(json.dumps(d['config']))" "$CONFIG_FILE")
    curl -sf -X PUT \
        -H "Content-Type: application/json" \
        -d "$CONFIG_ONLY" \
        "${CONNECT_URL}/connectors/${CONNECTOR_NAME}/config" | python3 -m json.tool 2>/dev/null || true
    echo "Connector '${CONNECTOR_NAME}' updated."
else
    echo "Creating connector '${CONNECTOR_NAME}'..."
    curl -sf -X POST \
        -H "Content-Type: application/json" \
        -d @"$CONFIG_FILE" \
        "${CONNECT_URL}/connectors" | python3 -m json.tool 2>/dev/null || true
    echo "Connector '${CONNECTOR_NAME}' created."
fi

# 验证状态
echo ""
echo "=== Connector Status ==="
curl -sf "${CONNECT_URL}/connectors/${CONNECTOR_NAME}/status" | python3 -m json.tool 2>/dev/null || \
    curl -sf "${CONNECT_URL}/connectors/${CONNECTOR_NAME}/status"
echo ""
echo "Done."
