#!/bin/bash

# Phase 3: Deploy EnhancedSwapRouter
# This script deploys the EnhancedSwapRouter with multi-protocol support

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"
source "$PROJECT_ROOT/scripts/load-env.sh" >/dev/null 2>&1 || true

echo "=== Phase 3: Deploy EnhancedSwapRouter ==="

# Check required variables
REQUIRED_VARS=(
    "PRIVATE_KEY"
    "ETHEREUM_MAINNET_RPC"
    "ETHERSCAN_API_KEY"
)

for var in "${REQUIRED_VARS[@]}"; do
    if [ -z "${!var}" ] || [ "${!var}" == "0x..." ] || [ "${!var}" == "your_etherscan_api_key" ]; then
        echo "Error: $var is not set in .env"
        exit 1
    fi
done

echo ""
echo "--- Deploying EnhancedSwapRouter ---"

forge script script/bridge/trustless/DeployEnhancedSwapRouter.s.sol:DeployEnhancedSwapRouter \
    --rpc-url "$ETHEREUM_MAINNET_RPC" \
    --broadcast \
    --via-ir \
    --verify \
    --etherscan-api-key "$ETHERSCAN_API_KEY" \
    --private-key "$PRIVATE_KEY"

echo ""
echo "⚠️  Please save ENHANCED_SWAP_ROUTER address to .env file"
echo "   Extract from deployment output above"

echo ""
echo "--- Configuring Balancer Pool IDs (Optional) ---"
echo "If you have Balancer pool IDs, configure them now:"
echo ""
echo "cast send \$ENHANCED_SWAP_ROUTER \\"
echo "  \"setBalancerPoolId(address,address,bytes32)\" \\"
echo "  <tokenA> <tokenB> <poolId> \\"
echo "  --rpc-url \$ETHEREUM_MAINNET_RPC \\"
echo "  --private-key \$PRIVATE_KEY"

echo ""
echo "=== Phase 3 Complete ==="
echo "EnhancedSwapRouter deployed"
echo "⚠️  Remember to update .env with ENHANCED_SWAP_ROUTER address"
