#!/usr/bin/env bash
# Generate comprehensive Mainnet deployment plan

set -e

cd "$(dirname "$0")/../.."

echo "=== Mainnet Deployment Plan ==="

# Color codes

# Get wallet balance
WALLET_BALANCE=$(./scripts/deployment/check-mainnet-balances.sh 2>&1 | grep -oP 'Balance: \K[0-9.]+' | head -1)

# Get gas prices
GAS_OUTPUT=$(./scripts/deployment/get-mainnet-gas-prices.sh 2>&1)
CONSERVATIVE_GAS=$(echo "$GAS_OUTPUT" | grep -oP 'Recommended Gas Price.*: \K[0-9.]+' | head -1)

log_info "Current Status:"
echo "  Wallet Balance: $WALLET_BALANCE ETH"
echo "  Gas Price: $CONSERVATIVE_GAS Gwei"

log_success "Remaining Contracts for Mainnet Deployment:"

# Contract information
cat << 'CONTRACTS'
1. CCIPLogger
   - Location: contracts/ccip-integration/CCIPLogger.sol
   - Framework: Hardhat
   - Estimated Cost: ~0.008 ETH
   - Dependencies: None
   - Priority: HIGH (can deploy independently)

2. CCIPWETH9Bridge
   - Location: contracts/ccip/CCIPWETH9Bridge.sol
   - Framework: Foundry
   - Estimated Cost: ~0.006 ETH
   - Dependencies: CCIPRouter
   - Priority: MEDIUM (requires CCIPRouter)

3. CCIPWETH10Bridge
   - Location: contracts/ccip/CCIPWETH10Bridge.sol
   - Framework: Foundry
   - Estimated Cost: ~0.006 ETH
   - Dependencies: CCIPRouter
   - Priority: MEDIUM (requires CCIPRouter)
CONTRACTS

log_warn "Deployment Prioritization:"

TOTAL_COST=0.020

if (( $(echo "$WALLET_BALANCE >= $TOTAL_COST" | bc -l 2>/dev/null || echo "0") )); then
    log_success "✅ Sufficient balance for all deployments"
    echo "Recommended deployment order:"
    echo "  1. CCIPLogger (no dependencies)"
    echo "  2. CCIPWETH9Bridge (after CCIPRouter)"
    echo "  3. CCIPWETH10Bridge (after CCIPRouter)"
else
    log_error "⚠️  Insufficient balance for all deployments"
    NEEDED=$(echo "$TOTAL_COST - $WALLET_BALANCE" | bc 2>/dev/null || echo "0")
    echo "  Additional ETH needed: $NEEDED ETH"
    echo "Prioritized deployment order (based on available balance):"
    
    # Calculate what can be deployed
    if (( $(echo "$WALLET_BALANCE >= 0.008" | bc -l 2>/dev/null || echo "0") )); then
        echo "  1. ✅ CCIPLogger ($(echo "$WALLET_BALANCE - 0.008" | bc) ETH remaining)"
        REMAINING=$(echo "$WALLET_BALANCE - 0.008" | bc 2>/dev/null || echo "0")
        if (( $(echo "$REMAINING >= 0.006" | bc -l 2>/dev/null || echo "0") )); then
            echo "  2. ✅ CCIPWETH9Bridge ($(echo "$REMAINING - 0.006" | bc) ETH remaining)"
            REMAINING=$(echo "$REMAINING - 0.006" | bc 2>/dev/null || echo "0")
            if (( $(echo "$REMAINING >= 0.006" | bc -l 2>/dev/null || echo "0") )); then
                echo "  3. ✅ CCIPWETH10Bridge"
            else
                echo "  3. ❌ CCIPWETH10Bridge (insufficient funds)"
            fi
        else
            echo "  2. ❌ CCIPWETH9Bridge (insufficient funds)"
            echo "  3. ❌ CCIPWETH10Bridge (insufficient funds)"
        fi
    else
        echo "  1. ❌ CCIPLogger (insufficient funds)"
        echo "  2. ❌ CCIPWETH9Bridge (insufficient funds)"
        echo "  3. ❌ CCIPWETH10Bridge (insufficient funds)"
    fi
fi

log_info "Next Steps:"
echo "  1. Run: ./scripts/deployment/compile-test-mainnet-contracts.sh"
echo "  2. Review: ./scripts/deployment/prioritize-mainnet-deployments.sh"
echo "  3. Deploy: Follow prioritized order above"
