#!/bin/bash
# =============================================================================
# Debezium Connector Registration Script
# =============================================================================
# Usage: ./register-connectors.sh
#
# This script registers all PostgreSQL connectors for CDC:
# - 1.0 系统: identity, planting, referral, wallet, authorization
# - 2.0 系统: auth, contribution, mining, trading, mining-wallet (outbox tables)
# =============================================================================

set -e

CONNECT_URL="${DEBEZIUM_CONNECT_URL:-http://localhost:8083}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MAX_RETRIES=30
RETRY_INTERVAL=5

echo "=== Debezium Connector Registration ==="
echo "Connect URL: $CONNECT_URL"
echo "Script Dir: $SCRIPT_DIR"

# Wait for Debezium Connect to be ready
echo ""
echo "Waiting for Debezium Connect to be ready..."
for i in $(seq 1 $MAX_RETRIES); do
    if curl -s "$CONNECT_URL/" > /dev/null 2>&1; then
        echo "Debezium Connect is ready!"
        break
    fi
    if [ $i -eq $MAX_RETRIES ]; then
        echo "ERROR: Debezium Connect is not ready after $MAX_RETRIES attempts"
        exit 1
    fi
    echo "Attempt $i/$MAX_RETRIES - waiting ${RETRY_INTERVAL}s..."
    sleep $RETRY_INTERVAL
done

# Check existing connectors
echo ""
echo "Checking existing connectors..."
EXISTING=$(curl -s "$CONNECT_URL/connectors")
echo "Existing connectors: $EXISTING"

# Function to register a connector from JSON file
register_connector() {
    local json_file="$1"
    local connector_name="$2"

    if [ ! -f "$json_file" ]; then
        echo "WARNING: $json_file not found, skipping..."
        return
    fi

    echo ""
    echo "Registering $connector_name..."

    # Delete existing connector if exists
    if echo "$EXISTING" | grep -q "$connector_name"; then
        echo "  Deleting existing $connector_name..."
        curl -s -X DELETE "$CONNECT_URL/connectors/$connector_name"
        sleep 2
    fi

    # Register connector
    RESULT=$(curl -s -X POST \
        -H "Content-Type: application/json" \
        -d @"$json_file" \
        "$CONNECT_URL/connectors")

    echo "  Result: $RESULT"

    # Check status
    sleep 2
    STATUS=$(curl -s "$CONNECT_URL/connectors/$connector_name/status" 2>/dev/null || echo "Status check failed")
    echo "  Status: $STATUS"
}

# =============================================================================
# 1.0 系统 Connectors (监听业务表)
# =============================================================================
echo ""
echo "=== Registering 1.0 System Connectors ==="

register_connector "$SCRIPT_DIR/identity-connector.json" "identity-postgres-connector"
register_connector "$SCRIPT_DIR/planting-connector.json" "planting-postgres-connector"
register_connector "$SCRIPT_DIR/referral-connector.json" "referral-postgres-connector"
register_connector "$SCRIPT_DIR/wallet-connector.json" "wallet-postgres-connector"
register_connector "$SCRIPT_DIR/authorization-connector.json" "authorization-postgres-connector"

# =============================================================================
# 2.0 系统 Connectors (监听 outbox_events 表)
# =============================================================================
echo ""
echo "=== Registering 2.0 System Outbox Connectors ==="

register_connector "$SCRIPT_DIR/auth-outbox-connector.json" "auth-outbox-connector"
register_connector "$SCRIPT_DIR/contribution-outbox-connector.json" "contribution-outbox-connector"
register_connector "$SCRIPT_DIR/mining-outbox-connector.json" "mining-outbox-connector"
register_connector "$SCRIPT_DIR/trading-outbox-connector.json" "trading-outbox-connector"
register_connector "$SCRIPT_DIR/mining-wallet-outbox-connector.json" "mining-wallet-outbox-connector"

# =============================================================================
# Summary
# =============================================================================
echo ""
echo "=== Registration Complete ==="
echo ""
echo "Kafka topics created:"
echo ""
echo "1.0 System (业务表 CDC):"
echo "  - cdc.identity.public.user_accounts"
echo "  - cdc.planting.public.planting_orders"
echo "  - cdc.referral.public.referral_relationships"
echo "  - cdc.wallet.public.wallet_accounts"
echo "  - cdc.authorization.public.*"
echo ""
echo "2.0 System (Outbox 表 CDC):"
echo "  - cdc.auth.outbox"
echo "  - cdc.contribution.outbox"
echo "  - cdc.mining.outbox"
echo "  - cdc.trading.outbox"
echo "  - cdc.mining-wallet.outbox"
echo ""
echo "To verify:"
echo "  curl $CONNECT_URL/connectors"
echo "  curl $CONNECT_URL/connectors/<connector-name>/status"
