#!/usr/bin/env bash
# Deploy bridges on Chain-138

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 "=== Deploying Bridges on Chain-138 ==="

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

CHAIN138_RPC="${RPC_URL:-https://rpc.d-bis.org}"
PRIVATE_KEY="${PRIVATE_KEY}"
if [[ ! "$PRIVATE_KEY" =~ ^0x ]]; then
    PRIVATE_KEY="0x$PRIVATE_KEY"
fi

WETH9_ADDRESS="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
WETH10_ADDRESS="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F"
CCIP_ROUTER="${CCIP_ROUTER:-}"
CCIP_FEE_TOKEN="${CCIP_FEE_TOKEN:-0x0000000000000000000000000000000000000000}"

if [ -z "$CCIP_ROUTER" ]; then
    log_warn "⚠️  CCIP_ROUTER not set in .env"
    echo "Please set CCIP_ROUTER to the Chain-138 CCIP Router address"
    exit 1
fi

echo "Deploying CCIPWETH9Bridge..."
export CCIP_ROUTER
export CCIP_FEE_TOKEN
WETH9_BRIDGE=$(forge script script/DeployCCIPWETH9Bridge.s.sol --rpc-url "$CHAIN138_RPC" --broadcast --private-key "$PRIVATE_KEY" --legacy 2>&1 | grep -E "Deployed to:|Contract deployed at:" | tail -1 | sed 's/.*at: //' | awk '{print $1}' | tr -d '\n' || echo "")

if [ -n "$WETH9_BRIDGE" ] && [ "$WETH9_BRIDGE" != "" ]; then
    log_success "✅ CCIPWETH9Bridge deployed: $WETH9_BRIDGE"
    if grep -q "CCIPWETH9_BRIDGE_CHAIN138=" "$PROJECT_ROOT/.env" 2>/dev/null; then
        sed -i "s|CCIPWETH9_BRIDGE_CHAIN138=.*|CCIPWETH9_BRIDGE_CHAIN138=$WETH9_BRIDGE|" "$PROJECT_ROOT/.env"
    else
        echo "CCIPWETH9_BRIDGE_CHAIN138=$WETH9_BRIDGE" >> "$PROJECT_ROOT/.env"
    fi
else
    log_warn "⚠️  Deployment may have failed"
fi

echo "Deploying CCIPWETH10Bridge..."
WETH10_BRIDGE=$(forge script script/DeployCCIPWETH10Bridge.s.sol --rpc-url "$CHAIN138_RPC" --broadcast --private-key "$PRIVATE_KEY" --legacy 2>&1 | grep -E "Deployed to:|Contract deployed at:" | tail -1 | sed 's/.*at: //' | awk '{print $1}' | tr -d '\n' || echo "")

if [ -n "$WETH10_BRIDGE" ] && [ "$WETH10_BRIDGE" != "" ]; then
    log_success "✅ CCIPWETH10Bridge deployed: $WETH10_BRIDGE"
    if grep -q "CCIPWETH10_BRIDGE_CHAIN138=" "$PROJECT_ROOT/.env" 2>/dev/null; then
        sed -i "s|CCIPWETH10_BRIDGE_CHAIN138=.*|CCIPWETH10_BRIDGE_CHAIN138=$WETH10_BRIDGE|" "$PROJECT_ROOT/.env"
    else
        echo "CCIPWETH10_BRIDGE_CHAIN138=$WETH10_BRIDGE" >> "$PROJECT_ROOT/.env"
    fi
else
    log_warn "⚠️  Deployment may have failed"
fi

log_success "✅ Chain-138 bridge deployment complete!"
