#!/bin/bash

# Verify PRIVATE_KEY is set and valid

set -e

cd /home/intlc/projects/proxmox/smom-dbis-138

# Load .env
if [ -f .env ]; then
    source .env
else
    echo "❌ .env file not found"
    exit 1
fi

# Check PRIVATE_KEY
if [ -z "$PRIVATE_KEY" ]; then
    echo "❌ PRIVATE_KEY not set in .env"
    exit 1
fi

echo "✅ PRIVATE_KEY is set"
echo "Verifying format and deployer address..."

# Get deployer address
DEPLOYER=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "")

if [ -z "$DEPLOYER" ]; then
    echo "❌ Invalid PRIVATE_KEY format"
    exit 1
fi

echo "✅ PRIVATE_KEY is valid"
echo "Deployer address: $DEPLOYER"

# Check balance
RPC_URL=${RPC_URL:-http://192.168.11.250:8545}
BALANCE=$(cast balance "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

if [ "$BALANCE" != "0" ]; then
    BALANCE_ETH=$(echo "scale=4; $BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0")
    echo "Balance: $BALANCE_ETH ETH"
    
    if (( $(echo "$BALANCE_ETH < 0.1" | bc -l 2>/dev/null || echo 1) )); then
        echo "⚠️  Warning: Low balance. Recommended: 0.1+ ETH for deployments"
    else
        echo "✅ Sufficient balance for deployments"
    fi
else
    echo "⚠️  Could not check balance (RPC may be unavailable)"
fi

echo ""
echo "✅ PRIVATE_KEY configuration complete!"
echo "Ready for deployment: ./scripts/deploy-and-integrate-all.sh"

