#!/usr/bin/env bash

set -e

# Deploy all WETH contracts (WETH9, WETH10) and CCIP bridges to ChainID 138

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

RPC_URL="${RPC_URL:-}"
PRIVATE_KEY="${PRIVATE_KEY:-}"

# CCIP Configuration
CCIP_ROUTER="${CCIP_ROUTER:-}"
CCIP_FEE_TOKEN="${CCIP_FEE_TOKEN:-}"  # LINK token address

# Deployment flags (default to true if not set)
DEPLOY_WETH9="${DEPLOY_WETH9:-true}"
DEPLOY_WETH10="${DEPLOY_WETH10:-true}"
DEPLOY_BRIDGES="${DEPLOY_BRIDGES:-true}"

# Optional: Use existing WETH addresses instead of deploying
WETH9_ADDRESS="${WETH9_ADDRESS:-}"
WETH10_ADDRESS="${WETH10_ADDRESS:-}"

cd "$PROJECT_ROOT"
# 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

RPC_URL="${RPC_URL:-${RPC_URL_138:-${CHAIN138_RPC_URL:-http://localhost:8545}}}"

if ! require_private_key_env; then
    exit 1
fi

if [ "$DEPLOY_BRIDGES" = "true" ]; then
    if [ -z "$CCIP_ROUTER" ]; then
        echo "ERROR: CCIP_ROUTER not available. Set it in smom-dbis-138/.env or repo .env." >&2
        exit 1
    fi
    if [ -z "$CCIP_FEE_TOKEN" ]; then
        echo "ERROR: CCIP_FEE_TOKEN not available. Set it in smom-dbis-138/.env or repo .env." >&2
        exit 1
    fi
fi

echo "========================================="
echo "Deploying WETH Contracts with CCIP"
echo "========================================="
echo "RPC URL: $RPC_URL"
echo "Chain ID: 138"
echo "Deployment Configuration:"
echo "  Deploy WETH9: $DEPLOY_WETH9"
echo "  Deploy WETH10: $DEPLOY_WETH10"
echo "  Deploy Bridges: $DEPLOY_BRIDGES"
if [ "$DEPLOY_BRIDGES" = "true" ]; then
    echo "CCIP Configuration:"
    echo "  CCIP Router: $CCIP_ROUTER"
    echo "  Fee Token (LINK): $CCIP_FEE_TOKEN"
fi
if [ -n "$WETH9_ADDRESS" ]; then
    echo "Using existing WETH9: $WETH9_ADDRESS"
fi
if [ -n "$WETH10_ADDRESS" ]; then
    echo "Using existing WETH10: $WETH10_ADDRESS"
fi
echo "========================================="

# Export environment variables for Foundry
export PRIVATE_KEY
export CCIP_ROUTER
export CCIP_FEE_TOKEN
export DEPLOY_WETH9
export DEPLOY_WETH10
export DEPLOY_BRIDGES
if [ -n "$WETH9_ADDRESS" ]; then
    export WETH9_ADDRESS
fi
if [ -n "$WETH10_ADDRESS" ]; then
    export WETH10_ADDRESS
fi

# Deploy all contracts
forge script script/DeployWETHWithCCIP.s.sol:DeployWETHWithCCIP \
    --rpc-url "$RPC_URL" \
    --broadcast \
    --private-key "$PRIVATE_KEY" \
    --verify \
    -vvvv

echo "========================================="
echo "WETH with CCIP Deployment Complete!"
echo "========================================="
echo "Next steps:"
echo "1. Save the deployed addresses to your .env file"
echo "2. Configure bridge destinations:"
echo "   ./scripts/deployment/configure-weth9-bridge.sh"
echo "   ./scripts/deployment/configure-weth10-bridge.sh"
echo "3. Test cross-chain transfers"
