#!/bin/bash
# =============================================================================
# Debezium Connector Registration Script
# =============================================================================
# Usage: ./register-connectors.sh
#
# This script registers the PostgreSQL connector for identity-service CDC
# It should be run after Debezium Connect is fully started
# =============================================================================

set -e

CONNECT_URL="${DEBEZIUM_CONNECT_URL:-http://localhost:8083}"
MAX_RETRIES=30
RETRY_INTERVAL=5

echo "=== Debezium Connector Registration ==="
echo "Connect URL: $CONNECT_URL"

# Wait for Debezium Connect to be ready
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"

# Register identity-postgres connector
echo ""
echo "Registering identity-postgres-connector..."

CONNECTOR_CONFIG='{
  "name": "identity-postgres-connector",
  "config": {
    "connector.class": "io.debezium.connector.postgresql.PostgresConnector",
    "tasks.max": "1",

    "database.hostname": "postgres",
    "database.port": "5432",
    "database.user": "'${POSTGRES_USER:-rwa_user}'",
    "database.password": "'${POSTGRES_PASSWORD:-rwa_secure_password}'",
    "database.dbname": "rwa_identity",
    "database.server.name": "identity",

    "topic.prefix": "cdc.identity",

    "table.include.list": "public.user_accounts",

    "plugin.name": "pgoutput",
    "publication.name": "debezium_identity_publication",
    "publication.autocreate.mode": "filtered",

    "slot.name": "debezium_identity_slot",

    "key.converter": "org.apache.kafka.connect.json.JsonConverter",
    "key.converter.schemas.enable": "false",
    "value.converter": "org.apache.kafka.connect.json.JsonConverter",
    "value.converter.schemas.enable": "false",

    "transforms": "unwrap",
    "transforms.unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
    "transforms.unwrap.drop.tombstones": "true",
    "transforms.unwrap.delete.handling.mode": "rewrite",
    "transforms.unwrap.add.fields": "op,table,source.ts_ms",

    "heartbeat.interval.ms": "10000",

    "snapshot.mode": "initial",

    "decimal.handling.mode": "string",
    "time.precision.mode": "connect"
  }
}'

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

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

echo "Result: $RESULT"

# Check connector status
echo ""
echo "Checking connector status..."
sleep 3
STATUS=$(curl -s "$CONNECT_URL/connectors/identity-postgres-connector/status")
echo "Connector status: $STATUS"

echo ""
echo "=== Registration Complete ==="
echo ""
echo "Kafka topics created:"
echo "  - cdc.identity.public.user_accounts"
echo ""
echo "To verify:"
echo "  curl $CONNECT_URL/connectors"
echo "  curl $CONNECT_URL/connectors/identity-postgres-connector/status"
