#!/bin/bash
# Complete fix for all issues - including key generation
# Usage: ./scripts/fix-all-issues-complete.sh

set -e

PROXMOX_HOST="192.168.11.11"
WEB3SIGNER_IP="192.168.11.111"
REDIS_CONTAINER="106"
VAULT_CONTAINER="108"
WEB3SIGNER_CONTAINER="107"

echo "═══════════════════════════════════════════════════════════════"
echo "🔧 COMPLETE FIX - ALL ISSUES"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Step 1: Fix Redis - Check installation and configuration
echo "Step 1: Fixing Redis (VMID 106)..."
echo "  Checking if Redis is installed..."
if ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- which redis-server" 2>/dev/null | grep -q redis-server; then
    echo "  ✅ Redis is installed"
    
    # Check if service exists
    if ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- systemctl list-unit-files | grep redis" 2>/dev/null | grep -q redis; then
        echo "  Starting Redis service..."
        ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- systemctl start redis-server && sleep 3" 2>&1
        ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- systemctl enable redis-server" 2>&1
    else
        echo "  ⚠️  Redis service not found, starting manually..."
        ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- redis-server --daemonize yes" 2>&1 || echo "  ⚠️  Could not start Redis"
    fi
    
    # Verify
    sleep 2
    REDIS_PING=$(ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- redis-cli ping" 2>&1 || echo "not responding")
    if [ "$REDIS_PING" = "PONG" ]; then
        echo "  ✅ Redis: Running (PONG)"
    else
        echo "  ⚠️  Redis: $REDIS_PING"
        echo "  Checking Redis logs..."
        ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- journalctl -u redis-server -n 10 --no-pager 2>&1" | tail -5 || echo "  No logs available"
    fi
else
    echo "  ⚠️  Redis not installed, installing..."
    ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- apt-get update && apt-get install -y redis-server" 2>&1
    ssh root@$PROXMOX_HOST "pct exec $REDIS_CONTAINER -- systemctl enable redis-server && systemctl start redis-server && sleep 3" 2>&1
fi

# Step 2: Fix Vault
echo ""
echo "Step 2: Fixing Vault (VMID 108)..."
echo "  Checking Vault status..."
VAULT_STATUS=$(ssh root@$PROXMOX_HOST "pct exec $VAULT_CONTAINER -- systemctl is-active vault" 2>&1 || echo "inactive")
if [ "$VAULT_STATUS" != "active" ]; then
    echo "  Starting Vault..."
    ssh root@$PROXMOX_HOST "pct exec $VAULT_CONTAINER -- systemctl start vault && sleep 3" 2>&1
    ssh root@$PROXMOX_HOST "pct exec $VAULT_CONTAINER -- systemctl enable vault" 2>&1
fi

# Verify Vault
sleep 2
VAULT_HEALTH=$(curl -s -m 5 http://192.168.11.112:8200/v1/sys/health 2>&1 | jq -r '.status' 2>/dev/null || echo "not responding")
if [ "$VAULT_HEALTH" = "active" ] || [ "$VAULT_HEALTH" = "standby" ]; then
    echo "  ✅ Vault: $VAULT_HEALTH"
else
    echo "  ⚠️  Vault: $VAULT_HEALTH"
    echo "  Checking Vault logs..."
    ssh root@$PROXMOX_HOST "pct exec $VAULT_CONTAINER -- journalctl -u vault -n 10 --no-pager 2>&1" | tail -5 || echo "  No logs available"
fi

# Step 3: Generate and load Web3Signer keys
echo ""
echo "Step 3: Generating and loading Web3Signer keys..."
KEYS_COUNT=$(ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- ls -1 /opt/web3signer/data/keys/*.json 2>/dev/null | wc -l" 2>&1 || echo "0")

if [ "$KEYS_COUNT" = "0" ]; then
    echo "  No keys found, generating new keys..."
    cd "$(dirname "$0")/.."
    
    # Check if Node.js is available
    if command -v node >/dev/null 2>&1 || command -v nodejs >/dev/null 2>&1; then
        echo "  Generating 3 test keystore files..."
        ./scripts/generate-test-keys.sh 3 TestWallet123! 2>&1 | tail -20
        
        # Check if keys were generated
        if [ -d "./keystores" ] && [ -n "$(ls -A ./keystores/*.json 2>/dev/null)" ]; then
            echo "  Keys generated, loading into Web3Signer..."
            
            # Copy keys to Proxmox host first
            echo "  Copying keys to Proxmox host..."
            mkdir -p /tmp/web3signer-keys
            cp ./keystores/*.json /tmp/web3signer-keys/ 2>/dev/null || echo "  ⚠️  Could not copy keys"
            
            # Load keys using the load script
            if [ -f "./scripts/load-keys-complete.sh" ]; then
                echo "  Loading keys into Web3Signer container..."
                ssh root@$PROXMOX_HOST 'bash -s' < ./scripts/load-keys-complete.sh 2>&1 | tail -20
            else
                echo "  ⚠️  Load script not found, loading manually..."
                ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- mkdir -p /opt/web3signer/data/keys" 2>&1
                for keyfile in ./keystores/keystore-*.json; do
                    if [ -f "$keyfile" ]; then
                        filename=$(basename "$keyfile")
                        ssh root@$PROXMOX_HOST "pct push $WEB3SIGNER_CONTAINER $keyfile /opt/web3signer/data/keys/$filename" 2>&1
                        echo "    ✅ Copied: $filename"
                    fi
                done
                ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- bash -c 'for f in /opt/web3signer/data/keys/*.json; do [ -f \"\$f\" ] && chmod 644 \"\$f\"; done'" 2>&1
            fi
            
            # Restart Web3Signer
            echo "  Restarting Web3Signer..."
            ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- systemctl daemon-reload && systemctl enable web3signer.service && systemctl restart web3signer.service && sleep 5" 2>&1
        else
            echo "  ⚠️  Key generation failed or keys not found"
        fi
    else
        echo "  ⚠️  Node.js not found, cannot generate keys"
        echo "  Please install Node.js or generate keys manually"
    fi
else
    echo "  ✅ Keys found: $KEYS_COUNT"
    echo "  Ensuring Web3Signer is running and loading keys..."
    ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- systemctl daemon-reload && systemctl enable web3signer.service && systemctl restart web3signer.service && sleep 5" 2>&1
fi

# Step 4: Verify keys loaded
echo ""
echo "Step 4: Verifying keys are loaded..."
sleep 3
KEYS=$(curl -s http://$WEB3SIGNER_IP:9000/api/v1/eth1/publicKeys 2>&1 || echo "[]")
if [ "$KEYS" != "[]" ] && [ -n "$KEYS" ]; then
    KEY_COUNT=$(echo "$KEYS" | jq '. | length' 2>/dev/null || echo "0")
    if [ "$KEY_COUNT" -gt 0 ]; then
        echo "  ✅ Keys loaded: $KEY_COUNT"
        ADDRESSES=$(echo "$KEYS" | jq -r '.[]' | tr '\n' ',' | sed 's/,$//')
        echo "$ADDRESSES" > /tmp/web3signer-addresses.txt
        echo "  Addresses: $ADDRESSES"
    else
        echo "  ⚠️  Keys not loaded yet"
    fi
else
    echo "  ⚠️  Keys not loaded"
    echo "  Checking Web3Signer logs..."
    ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- journalctl -u web3signer.service -n 20 --no-pager" 2>&1 | tail -10
fi

# Step 5: Configure allowlist
echo ""
echo "Step 5: Configuring wallet allowlist..."
if [ -f /tmp/web3signer-addresses.txt ]; then
    ADDRESSES=$(cat /tmp/web3signer-addresses.txt)
elif [ "$KEYS" != "[]" ] && [ -n "$KEYS" ]; then
    ADDRESSES=$(echo "$KEYS" | jq -r '.[]' | tr '\n' ',' | sed 's/,$//')
fi

if [ -n "$ADDRESSES" ] && [ "$ADDRESSES" != "" ]; then
    echo "  Configuring allowlist with addresses: $ADDRESSES"
    cd "$(dirname "$0")/.."
    ./scripts/configure-wallet-allowlist.sh "$ADDRESSES" 2>&1 || echo "  ⚠️  Allowlist configuration had issues"
    
    # Restart translators
    echo "  Restarting translator services..."
    for IP in 192.168.11.240 192.168.11.241 192.168.11.242; do
        ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@$IP "systemctl restart rpc-translator-138.service && sleep 2" 2>&1 || echo "  ⚠️  Failed to restart $IP"
    done
else
    echo "  ⚠️  No addresses available, skipping allowlist configuration"
fi

# Final status
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "📊 FINAL STATUS"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Run the status check
cd "$(dirname "$0")/.."
./scripts/check-all-status.sh 2>&1 | tail -30

echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "✅ COMPLETE FIX FINISHED"
echo "═══════════════════════════════════════════════════════════════"
