#!/usr/bin/env bash
# Comprehensive deployment readiness report

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi


log_info "=== Ethereum Mainnet Deployment Readiness Report ==="

# Load environment
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
fi

log_info "1. Contracts Status:"
echo "   ✅ CCIPWETH9Bridge - Ready (compiles with Foundry)"
echo "   ✅ CCIPWETH10Bridge - Ready (compiles with Foundry)"
echo "   ⚠️  CCIPLogger - Uses Hardhat (separate deployment)"

log_info "2. Environment Variables:"
[ -n "$PRIVATE_KEY" ] && echo "   ✅ PRIVATE_KEY - Set" || echo "   ❌ PRIVATE_KEY - Missing"
[ -n "$ETHEREUM_MAINNET_RPC" ] && echo "   ✅ ETHEREUM_MAINNET_RPC - Set" || echo "   ❌ ETHEREUM_MAINNET_RPC - Missing"
[ -n "$CCIP_ETH_ROUTER" ] && echo "   ✅ CCIP_ETH_ROUTER - Set" || echo "   ⚠️  CCIP_ETH_ROUTER - Using default"
[ -n "$CCIP_FEE_TOKEN" ] && echo "   ✅ CCIP_FEE_TOKEN - Set" || echo "   ⚠️  CCIP_FEE_TOKEN - Using default"

log_info "3. Wallet Status:"
WALLET_ADDRESS=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "")
if [ -n "$WALLET_ADDRESS" ]; then
    echo "   Address: $WALLET_ADDRESS"
    BALANCE=$(cast balance "$WALLET_ADDRESS" --rpc-url "https://eth.llamarpc.com" 2>/dev/null || echo "0")
    BALANCE_ETH=$(echo "scale=6; $BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0")
    if (( $(echo "$BALANCE_ETH > 0.001" | bc -l) )); then
        echo -e "   ${GREEN}✅ Balance: $BALANCE_ETH ETH${NC}"
    else
        echo -e "   ${RED}❌ Balance: $BALANCE_ETH ETH (insufficient)${NC}"
        echo "   ⚠️  Need at least 0.001 ETH for deployment"
    fi
else
    echo -e "   ${RED}❌ Could not derive wallet address${NC}"
fi

log_info "4. Gas Cost Estimates:"
if [ -f "$SCRIPT_DIR/calculate-conservative-costs.sh" ]; then
    "$SCRIPT_DIR/calculate-conservative-costs.sh" 2>&1 | grep -A 5 "Total"
else
    echo "   ⚠️  Cost estimation script not found"
fi

log_info "5. Deployment Scripts:"
[ -f "$PROJECT_ROOT/script/DeployCCIPWETH9Bridge.s.sol" ] && echo "   ✅ DeployCCIPWETH9Bridge.s.sol" || echo "   ❌ DeployCCIPWETH9Bridge.s.sol missing"
[ -f "$PROJECT_ROOT/script/DeployCCIPWETH10Bridge.s.sol" ] && echo "   ✅ DeployCCIPWETH10Bridge.s.sol" || echo "   ❌ DeployCCIPWETH10Bridge.s.sol missing"
[ -f "$PROJECT_ROOT/scripts/ccip-deployment/deploy-ccip-logger.js" ] && echo "   ✅ deploy-ccip-logger.js" || echo "   ❌ deploy-ccip-logger.js missing"
[ -f "$PROJECT_ROOT/scripts/deployment/deploy-all-mainnet.sh" ] && echo "   ✅ deploy-all-mainnet.sh" || echo "   ❌ deploy-all-mainnet.sh missing"

log_info "6. Next Steps:"
echo "   1. Fund wallet with sufficient ETH (see balance above)"
echo "   2. Run: ./scripts/deployment/deploy-all-mainnet.sh"
echo "   3. Verify contracts on Etherscan"
echo "   4. Configure bridge destinations"
