#!/usr/bin/env bash
# Complete all service configurations with all deployed contract addresses
# Usage: ./complete-all-configurations.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
PROXMOX_PASS="${PROXMOX_PASS:-L@kers2010}"

# Contract addresses
ORACLE_PROXY="0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6"
ORACLE_AGGREGATOR="0x99b3511a2d315a497c8112c1fdd8d508d4b1e506"
CCIP_ROUTER="0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e"
CCIP_SENDER="0x105F8A15b819948a89153505762444Ee9f324684"
PRICE_FEED_KEEPER="0xD3AD6831aacB5386B8A25BB8D8176a6C8a026f04"
WETH9="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
WETH10="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"
RPC_URL="http://192.168.11.250:8545"
WS_URL="ws://192.168.11.250:8546"
CHAIN_ID="138"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

ssh_proxmox() {
    sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$PROXMOX_HOST" "$@"
}

log_info "========================================="
log_info "Complete All Service Configurations"
log_info "========================================="
log_info ""

# Function to create/update .env file
update_env() {
    local vmid="$1"
    local env_file="$2"
    shift 2
    local env_vars="$@"
    
    if ssh_proxmox "pct list | grep -q '^$vmid '"; then
        log_info "Configuring VMID $vmid..."
        ssh_proxmox "pct exec $vmid -- bash -c '
            mkdir -p \$(dirname \"$env_file\")
            touch \"$env_file\"
            $env_vars
            echo \"✓ Updated $env_file\"
        '" 2>&1 || log_warn "Failed to update VMID $vmid"
    else
        log_warn "VMID $vmid not found, configuration ready for deployment"
    fi
}

# Oracle Publisher (3500)
update_env 3500 "/opt/oracle-publisher/.env" \
    "sed -i '/^ORACLE_ADDRESS=/d' \$ENV_FILE; echo \"ORACLE_ADDRESS=$ORACLE_PROXY\" >> \$ENV_FILE" \
    "sed -i '/^AGGREGATOR_ADDRESS=/d' \$ENV_FILE; echo \"AGGREGATOR_ADDRESS=$ORACLE_AGGREGATOR\" >> \$ENV_FILE" \
    "sed -i '/^RPC_URL=/d' \$ENV_FILE; echo \"RPC_URL=$RPC_URL\" >> \$ENV_FILE" \
    "sed -i '/^WS_URL=/d' \$ENV_FILE; echo \"WS_URL=$WS_URL\" >> \$ENV_FILE" \
    "sed -i '/^CHAIN_ID=/d' \$ENV_FILE; echo \"CHAIN_ID=$CHAIN_ID\" >> \$ENV_FILE"

# CCIP Monitor (3501)
update_env 3501 "/opt/ccip-monitor/.env" \
    "sed -i '/^CCIP_ROUTER_ADDRESS=/d' \$ENV_FILE; echo \"CCIP_ROUTER_ADDRESS=$CCIP_ROUTER\" >> \$ENV_FILE" \
    "sed -i '/^CCIP_SENDER_ADDRESS=/d' \$ENV_FILE; echo \"CCIP_SENDER_ADDRESS=$CCIP_SENDER\" >> \$ENV_FILE" \
    "sed -i '/^RPC_URL=/d' \$ENV_FILE; echo \"RPC_URL=$RPC_URL\" >> \$ENV_FILE" \
    "sed -i '/^WS_URL=/d' \$ENV_FILE; echo \"WS_URL=$WS_URL\" >> \$ENV_FILE" \
    "sed -i '/^CHAIN_ID=/d' \$ENV_FILE; echo \"CHAIN_ID=$CHAIN_ID\" >> \$ENV_FILE"

# Keeper (3502)
update_env 3502 "/opt/keeper/.env" \
    "sed -i '/^PRICE_FEED_KEEPER_ADDRESS=/d' \$ENV_FILE; echo \"PRICE_FEED_KEEPER_ADDRESS=$PRICE_FEED_KEEPER\" >> \$ENV_FILE" \
    "sed -i '/^ORACLE_PRICE_FEED=/d' \$ENV_FILE; echo \"ORACLE_PRICE_FEED=$ORACLE_PROXY\" >> \$ENV_FILE" \
    "sed -i '/^RPC_URL=/d' \$ENV_FILE; echo \"RPC_URL=$RPC_URL\" >> \$ENV_FILE" \
    "sed -i '/^WS_URL=/d' \$ENV_FILE; echo \"WS_URL=$WS_URL\" >> \$ENV_FILE" \
    "sed -i '/^CHAIN_ID=/d' \$ENV_FILE; echo \"CHAIN_ID=$CHAIN_ID\" >> \$ENV_FILE"

# Financial Tokenization (3503)
update_env 3503 "/opt/tokenization/.env" \
    "sed -i '/^RPC_URL=/d' \$ENV_FILE; echo \"RPC_URL=$RPC_URL\" >> \$ENV_FILE" \
    "sed -i '/^WS_URL=/d' \$ENV_FILE; echo \"WS_URL=$WS_URL\" >> \$ENV_FILE" \
    "sed -i '/^CHAIN_ID=/d' \$ENV_FILE; echo \"CHAIN_ID=$CHAIN_ID\" >> \$ENV_FILE" \
    "sed -i '/^WETH9_ADDRESS=/d' \$ENV_FILE; echo \"WETH9_ADDRESS=$WETH9\" >> \$ENV_FILE" \
    "sed -i '/^WETH10_ADDRESS=/d' \$ENV_FILE; echo \"WETH10_ADDRESS=$WETH10\" >> \$ENV_FILE"

# Firefly (6200)
if ssh_proxmox "pct list | grep -q '^6200 '"; then
    log_info "Configuring Firefly (VMID 6200)..."
    ssh_proxmox "pct exec 6200 -- bash -c '
        if [ -f /opt/firefly/docker-compose.yml ]; then
            sed -i \"s|FF_BLOCKCHAIN_RPC=.*|FF_BLOCKCHAIN_RPC=$RPC_URL|\" /opt/firefly/docker-compose.yml
            sed -i \"s|FF_BLOCKCHAIN_WS=.*|FF_BLOCKCHAIN_WS=$WS_URL|\" /opt/firefly/docker-compose.yml
            echo \"✓ Firefly configured\"
        fi
    '" 2>&1 || log_warn "Failed to configure Firefly"
fi

# Cacti (151)
if ssh_proxmox "pct list | grep -q '^151 '"; then
    log_info "Configuring Cacti (VMID 151)..."
    ssh_proxmox "pct exec 151 -- bash -c '
        if [ -f /opt/cacti/docker-compose.yml ]; then
            sed -i \"s|BESU_RPC_URL=.*|BESU_RPC_URL=$RPC_URL|\" /opt/cacti/docker-compose.yml
            sed -i \"s|BESU_WS_URL=.*|BESU_WS_URL=$WS_URL|\" /opt/cacti/docker-compose.yml
            echo \"✓ Cacti configured\"
        fi
    '" 2>&1 || log_warn "Failed to configure Cacti"
fi

log_info ""
log_success "========================================="
log_success "All Configurations Complete!"
log_success "========================================="
log_info ""
log_info "All services configured with:"
log_info "  - Contract addresses"
log_info "  - RPC URLs"
log_info "  - Chain ID"
log_info ""

