#!/usr/bin/env bash
# Prepare for deployment - check all prerequisites

set -e

cd "$(dirname "$0")/../.."
source scripts/lib/forge-scope.sh

echo "=== Deployment Preparation Check ==="

# Check wallet balance
echo "1. Checking wallet balance..."
./scripts/deployment/get-wallet-address.sh 2>&1 | grep -A 5 "Wallet Address" || true
./scripts/deployment/check-mainnet-balances.sh 2>&1 | grep -E "(Balance|Status|Need)" || true

# Check RPC endpoints
echo ""
echo "2. Checking RPC endpoints..."
./scripts/deployment/check-rpc-status.sh 2>&1 | head -20 || true

# Check contract compilation
echo ""
echo "3. Checking contract compilation..."
if forge build 2>&1 | grep -q "Compiler run successful"; then
    echo "✅ Foundry contracts compile successfully"
else
    echo "⚠️  Foundry compilation issues detected"
fi

if npx hardhat compile 2>&1 | grep -q "Compiled successfully"; then
    echo "✅ Hardhat contracts compile successfully"
else
    echo "⚠️  Hardhat compilation issues detected"
fi

# Check environment variables
echo ""
echo "4. Checking environment variables..."
if [ -f .env ]; then
    required_vars=("PRIVATE_KEY" "ETHEREUM_MAINNET_RPC" "CHAIN138_RPC_URL")
    for var in "${required_vars[@]}"; do
        if grep -q "^${var}=" .env; then
            echo "✅ $var configured"
        else
            echo "⚠️  $var not configured"
        fi
    done
else
    echo "⚠️  .env file not found"
fi

echo ""
echo "=== Preparation Check Complete ==="
