#!/bin/bash
# Configure Supporting Services in Containers
# Usage: ./configure-services.sh

set -e

echo "========================================="
echo "Configuring Supporting Services"
echo "========================================="
echo ""

# Function to configure Redis
configure_redis() {
    echo "Configuring Redis (VMID 106)..."
    ssh root@192.168.11.11 "pct exec 106 -- bash -c '
        apt-get update -qq
        apt-get install -y redis-server >/dev/null 2>&1
        
        # Configure Redis
        sed -i \"s/^bind .*/bind 192.168.11.110/\" /etc/redis/redis.conf 2>/dev/null || echo \"bind 192.168.11.110\" >> /etc/redis/redis.conf
        sed -i \"s/^protected-mode .*/protected-mode yes/\" /etc/redis/redis.conf 2>/dev/null || echo \"protected-mode yes\" >> /etc/redis/redis.conf
        
        # Add password if set in env (would need to pass it)
        # For now, leave password optional
        
        systemctl enable redis-server
        systemctl restart redis-server
        sleep 2
        
        if systemctl is-active redis-server >/dev/null; then
            echo \"✅ Redis configured and running\"
            redis-cli -h 192.168.11.110 ping
        else
            echo \"❌ Redis failed to start\"
            systemctl status redis-server
            exit 1
        fi
    '" 2>&1
    echo ""
}

# Function to configure Web3Signer
configure_web3signer() {
    echo "Configuring Web3Signer (VMID 107)..."
    ssh root@192.168.11.11 "pct exec 107 -- bash -c '
        apt-get update -qq
        apt-get install -y openjdk-17-jre-headless wget curl >/dev/null 2>&1
        
        cd /opt
        if [ ! -d web3signer-23.10.0 ]; then
            wget -q https://artifacts.consensys.net/web3signer/web3signer-23.10.0/web3signer-23.10.0.tar.gz
            tar -xzf web3signer-23.10.0.tar.gz
        fi
        
        cd web3signer-23.10.0
        mkdir -p /opt/web3signer/data
        
        # Create basic config
        cat > web3signer.yml <<EOFC
server:
  http-listen-port: 9000
  http-listen-host: 192.168.11.111
  
data-path: /opt/web3signer/data
EOFC
        
        echo \"✅ Web3Signer installed\"
        ./bin/web3signer --version 2>&1 | head -1
    '" 2>&1
    echo ""
}

# Function to configure Vault
configure_vault() {
    echo "Configuring Vault (VMID 108)..."
    ssh root@192.168.11.11 "pct exec 108 -- bash -c '
        apt-get update -qq
        apt-get install -y unzip wget curl >/dev/null 2>&1
        
        if ! command -v vault >/dev/null 2>&1; then
            cd /tmp
            wget -q https://releases.hashicorp.com/vault/1.15.0/vault_1.15.0_linux_amd64.zip
            unzip -q vault_1.15.0_linux_amd64.zip
            mv vault /usr/local/bin/
            chmod +x /usr/local/bin/vault
        fi
        
        useradd -r -s /bin/false vault 2>/dev/null || true
        mkdir -p /etc/vault.d /var/lib/vault
        chown vault:vault /var/lib/vault
        
        echo \"✅ Vault installed\"
        vault version 2>&1 | head -1
    '" 2>&1
    echo ""
}

# Configure all services
configure_redis
configure_web3signer
configure_vault

echo "========================================="
echo "✅ Service Configuration Complete"
echo "========================================="
echo ""
echo "Next steps:"
echo "1. Configure service-specific settings (keys, policies, etc.)"
echo "2. Start services (Redis is already running)"
echo "3. Test connectivity"
echo "4. Deploy translator service"
