#!/usr/bin/env bash
# Restart services and verify they are working with new contract addresses
# Usage: ./restart-and-verify-services.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}"

# 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 "Restart and Verify Services"
log_info "========================================="
log_info ""

# Function to restart service in container
restart_service() {
    local vmid="$1"
    local service_name="$2"
    local service_type="${3:-systemd}"
    
    log_info "Restarting $service_name in VMID $vmid..."
    
    if [ "$service_type" = "systemd" ]; then
        ssh_proxmox "pct exec $vmid -- systemctl restart $service_name" 2>&1 && \
        ssh_proxmox "pct exec $vmid -- systemctl enable $service_name" 2>&1 && \
        sleep 2 && \
        ssh_proxmox "pct exec $vmid -- systemctl is-active $service_name" 2>&1 | grep -q "active" && {
            log_success "$service_name is active"
            return 0
        } || {
            log_warn "$service_name may not be active"
            return 1
        }
    elif [ "$service_type" = "docker" ]; then
        ssh_proxmox "pct exec $vmid -- docker-compose -f /opt/$service_name/docker-compose.yml restart" 2>&1 && {
            log_success "$service_name restarted"
            return 0
        } || {
            log_warn "$service_name restart may have failed"
            return 1
        }
    fi
}

# Function to verify service configuration
verify_service_config() {
    local vmid="$1"
    local env_file="$2"
    local expected_vars="$3"
    
    log_info "Verifying configuration in VMID $vmid..."
    
    local config=$(ssh_proxmox "pct exec $vmid -- cat $env_file 2>/dev/null" 2>&1)
    
    if [ -z "$config" ]; then
        log_warn "Configuration file not found: $env_file"
        return 1
    fi
    
    local missing=0
    for var in $expected_vars; do
        if echo "$config" | grep -q "^${var}="; then
            local value=$(echo "$config" | grep "^${var}=" | cut -d'=' -f2)
            log_info "  ✓ $var = $value"
        else
            log_warn "  ✗ $var not found"
            missing=$((missing + 1))
        fi
    done
    
    if [ $missing -eq 0 ]; then
        log_success "All required variables configured"
        return 0
    else
        log_warn "$missing variables missing"
        return 1
    fi
}

# Oracle Publisher Service (VMID 3500)
if ssh_proxmox "pct list | grep -q '3500'"; then
    log_info ""
    log_info "=== Oracle Publisher Service (VMID 3500) ==="
    verify_service_config 3500 "/opt/oracle-publisher/.env" "ORACLE_ADDRESS AGGREGATOR_ADDRESS RPC_URL CHAIN_ID"
    
    # Try to restart if service exists
    if ssh_proxmox "pct exec 3500 -- systemctl list-units | grep -q oracle-publisher"; then
        restart_service 3500 "oracle-publisher" "systemd"
    elif ssh_proxmox "pct exec 3500 -- [ -f /opt/oracle-publisher/docker-compose.yml ]"; then
        restart_service 3500 "oracle-publisher" "docker"
    else
        log_warn "Service type not detected, skipping restart"
    fi
else
    log_warn "VMID 3500 not found, skipping..."
fi

# CCIP Monitor Service (VMID 3501)
if ssh_proxmox "pct list | grep -q '3501'"; then
    log_info ""
    log_info "=== CCIP Monitor Service (VMID 3501) ==="
    verify_service_config 3501 "/opt/ccip-monitor/.env" "CCIP_ROUTER_ADDRESS CCIP_SENDER_ADDRESS RPC_URL CHAIN_ID"
    
    # Try to restart if service exists
    if ssh_proxmox "pct exec 3501 -- systemctl list-units | grep -q ccip-monitor"; then
        restart_service 3501 "ccip-monitor" "systemd"
    elif ssh_proxmox "pct exec 3501 -- [ -f /opt/ccip-monitor/docker-compose.yml ]"; then
        restart_service 3501 "ccip-monitor" "docker"
    else
        log_warn "Service type not detected, skipping restart"
    fi
else
    log_warn "VMID 3501 not found, skipping..."
fi

# Test Oracle contract connectivity
log_info ""
log_info "=== Testing Oracle Contract Connectivity ==="
ORACLE_PROXY="0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6"
RPC_URL="http://192.168.11.250:8545"

log_info "Testing RPC connection..."
RPC_TEST=$(curl -s -X POST "$RPC_URL" \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' 2>/dev/null)

if echo "$RPC_TEST" | grep -q '"result"'; then
    BLOCK=$(echo "$RPC_TEST" | python3 -c "import sys, json; data=json.load(sys.stdin); print(int(data.get('result', '0x0'), 16))" 2>/dev/null || echo "0")
    log_success "RPC is accessible (Block: $BLOCK)"
    
    log_info "Testing Oracle contract..."
    ORACLE_TEST=$(curl -s -X POST "$RPC_URL" \
        -H 'Content-Type: application/json' \
        -d "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getCode\",\"params\":[\"$ORACLE_PROXY\",\"latest\"],\"id\":1}" 2>/dev/null)
    
    if echo "$ORACLE_TEST" | grep -q '"result"' && ! echo "$ORACLE_TEST" | grep -q '"0x"'; then
        log_success "Oracle contract is deployed and accessible"
    else
        log_warn "Oracle contract may not be accessible"
    fi
else
    log_warn "RPC connection test failed"
fi

log_info ""
log_success "========================================="
log_success "Service Restart and Verification Complete!"
log_success "========================================="
log_info ""
log_info "Next steps:"
log_info "1. Check service logs for any errors"
log_info "2. Verify services are processing transactions"
log_info "3. Test MetaMask integration"
log_info ""

