#!/usr/bin/env bash

# Deploy CCIP WETH Bridges on Ethereum Mainnet

set -e

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


# Load environment variables
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
else
    log_error "Error: .env file not found"
    exit 1
fi

# Canonical addresses
WETH9_ADDRESS="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
WETH10_ADDRESS="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"

log_success "=== Deploying CCIP WETH Bridges on Ethereum Mainnet ==="

# Pre-deployment balance check
log_warn "Running pre-deployment balance check..."
if ! "$SCRIPT_DIR/check-wallet-balances.sh" > /dev/null 2>&1; then
    log_error "Error: Insufficient wallet balances. Please fund your wallet first."
    echo "Run: ./scripts/deployment/check-wallet-balances.sh for details"
    exit 1
fi
log_success "✅ Wallet balances sufficient"

# Check required variables
if [ -z "$MAINNET_RPC_URL" ]; then
    MAINNET_RPC_URL="https://eth.llamarpc.com"
    log_warn "Using default Mainnet RPC: $MAINNET_RPC_URL"
fi

if [ -z "$MAINNET_PRIVATE_KEY" ]; then
    if [ -n "$PRIVATE_KEY" ]; then
        MAINNET_PRIVATE_KEY="$PRIVATE_KEY"
        log_warn "Using PRIVATE_KEY for Mainnet deployment"
    else
        log_error "Error: MAINNET_PRIVATE_KEY or PRIVATE_KEY not set"
        exit 1
    fi
fi

if [ -z "$MAINNET_CCIP_ROUTER" ]; then
    log_error "Error: MAINNET_CCIP_ROUTER not set"
    exit 1
fi

if [ -z "$MAINNET_LINK_TOKEN" ]; then
    MAINNET_LINK_TOKEN="0x514910771AF9Ca656af840dff83E8264EcF986CA"
    log_warn "Using default Mainnet LINK: $MAINNET_LINK_TOKEN"
fi

echo "Configuration:"
echo "  RPC URL: $MAINNET_RPC_URL"
echo "  CCIP Router: $MAINNET_CCIP_ROUTER"
echo "  LINK Token: $MAINNET_LINK_TOKEN"
echo "  WETH9: $WETH9_ADDRESS"
echo "  WETH10: $WETH10_ADDRESS"

# Deploy CCIPWETH9Bridge
log_warn "Deploying CCIPWETH9Bridge..."
WETH9_BRIDGE=$(forge script script/DeployCCIPWETH9Bridge.s.sol \
    --rpc-url "$MAINNET_RPC_URL" \
    --broadcast \
    --constructor-args "$MAINNET_CCIP_ROUTER" "$WETH9_ADDRESS" "$MAINNET_LINK_TOKEN" \
    --private-key "$MAINNET_PRIVATE_KEY" \
    2>&1 | grep -E "Deployed to:|Contract deployed at:" | tail -1 | awk '{print $NF}' || echo "")

if [ -z "$WETH9_BRIDGE" ]; then
    log_error "Error: Failed to deploy CCIPWETH9Bridge"
    exit 1
fi

log_success "✅ CCIPWETH9Bridge deployed at: $WETH9_BRIDGE"

# Deploy CCIPWETH10Bridge
log_warn "Deploying CCIPWETH10Bridge..."
WETH10_BRIDGE=$(forge script script/DeployCCIPWETH10Bridge.s.sol \
    --rpc-url "$MAINNET_RPC_URL" \
    --broadcast \
    --constructor-args "$MAINNET_CCIP_ROUTER" "$WETH10_ADDRESS" "$MAINNET_LINK_TOKEN" \
    --private-key "$MAINNET_PRIVATE_KEY" \
    2>&1 | grep -E "Deployed to:|Contract deployed at:" | tail -1 | awk '{print $NF}' || echo "")

if [ -z "$WETH10_BRIDGE" ]; then
    log_error "Error: Failed to deploy CCIPWETH10Bridge"
    exit 1
fi

log_success "✅ CCIPWETH10Bridge deployed at: $WETH10_BRIDGE"

# Update .env file
log_warn "Updating .env file..."
if grep -q "MAINNET_CCIP_WETH9_BRIDGE=" "$PROJECT_ROOT/.env"; then
    sed -i "s|MAINNET_CCIP_WETH9_BRIDGE=.*|MAINNET_CCIP_WETH9_BRIDGE=$WETH9_BRIDGE|" "$PROJECT_ROOT/.env"
else
    echo "MAINNET_CCIP_WETH9_BRIDGE=$WETH9_BRIDGE" >> "$PROJECT_ROOT/.env"
fi

if grep -q "MAINNET_CCIP_WETH10_BRIDGE=" "$PROJECT_ROOT/.env"; then
    sed -i "s|MAINNET_CCIP_WETH10_BRIDGE=.*|MAINNET_CCIP_WETH10_BRIDGE=$WETH10_BRIDGE|" "$PROJECT_ROOT/.env"
else
    echo "MAINNET_CCIP_WETH10_BRIDGE=$WETH10_BRIDGE" >> "$PROJECT_ROOT/.env"
fi

log_success "✅ Deployment complete!"
echo "Bridge addresses:"
echo "  CCIPWETH9Bridge:  $WETH9_BRIDGE"
echo "  CCIPWETH10Bridge: $WETH10_BRIDGE"
