#!/bin/bash
# Prepare NODE1 for cluster mode

set -e

echo "=== Preparing NODE1 for Multi-Node Cluster ==="

# 1. Configure PostgreSQL for replication
echo "Configuring PostgreSQL..."
docker exec dagi-postgres psql -U postgres -c "ALTER SYSTEM SET wal_level = replica;"
docker exec dagi-postgres psql -U postgres -c "ALTER SYSTEM SET max_wal_senders = 10;"
docker exec dagi-postgres psql -U postgres -c "ALTER SYSTEM SET max_replication_slots = 10;"

# 2. Configure NATS for cluster
echo "Configuring NATS cluster..."
cat > /opt/microdao-daarion/nats-cluster.conf << EOF
port: 4222
http_port: 8222

jetstream {
    store_dir: /data/jetstream
    max_mem: 1G
    max_file: 10G
}

cluster {
    name: daarion-core
    listen: 0.0.0.0:6222
    routes: []
}
EOF

# 3. Export node configuration
echo "Creating node config..."
cat > /opt/microdao-daarion/node_config.yaml << EOF
node_id: node1
node_role: primary
services:
  - gateway
  - router
  - swapper
  - memory-service
  - crewai
data_stores:
  - postgresql (primary)
  - qdrant (shard1)
  - neo4j (core)
  - redis (master)
EOF

echo "✅ NODE1 prepared for clustering"
echo ""
echo "Next: Deploy NODE2 and configure as replica"
