#!/usr/bin/env bash

set -e

# Deploy WETH9 and WETH10 using CREATE2 to match genesis.json addresses

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
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

# Load environment

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

if ! require_private_key_env; then
    exit 1
fi

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🚀 Deploying WETH9 and WETH10 using CREATE2"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "RPC URL: $RPC_URL"
echo "Target Addresses:"
echo "  WETH9:  0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
echo "  WETH10: 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"
echo ""

# Compile contracts
echo "📦 Compiling contracts..."
forge build --force 2>&1 | grep -E "Compiler run|Error" || true

# Check if contracts are already deployed
echo ""
echo "🔍 Checking if contracts already exist..."
WETH9_CODE=$(cast code 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 --rpc-url "$RPC_URL" 2>/dev/null || echo "")
WETH10_CODE=$(cast code 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f --rpc-url "$RPC_URL" 2>/dev/null || echo "")

if [ -n "$WETH9_CODE" ] && [ "$WETH9_CODE" != "0x" ]; then
    echo "✅ WETH9 already deployed at 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
    WETH9_DEPLOYED=true
else
    echo "⚠️  WETH9 not deployed"
    WETH9_DEPLOYED=false
fi

if [ -n "$WETH10_CODE" ] && [ "$WETH10_CODE" != "0x" ]; then
    echo "✅ WETH10 already deployed at 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"
    WETH10_DEPLOYED=true
else
    echo "⚠️  WETH10 not deployed"
    WETH10_DEPLOYED=false
fi

# Deploy WETH9 if not deployed
if [ "$WETH9_DEPLOYED" = false ]; then
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "📤 Deploying WETH9 to exact address..."
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    
    forge script script/DeployWETH9ToExactAddress.s.sol:DeployWETH9ToExactAddress \
        --rpc-url "$RPC_URL" \
        --broadcast \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        -vvv 2>&1 | tee /tmp/weth9-deploy.log
    
    if grep -q "WETH9 deployed at: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" /tmp/weth9-deploy.log; then
        echo "✅ WETH9 deployed successfully!"
    else
        echo "⚠️  WETH9 deployment may have failed or used different address"
        echo "   Check /tmp/weth9-deploy.log for details"
    fi
else
    echo "⏭️  Skipping WETH9 deployment (already exists)"
fi

# Deploy WETH10 if not deployed
if [ "$WETH10_DEPLOYED" = false ]; then
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "📤 Deploying WETH10 to exact address..."
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    
    forge script script/DeployWETH10ToExactAddress.s.sol:DeployWETH10ToExactAddress \
        --rpc-url "$RPC_URL" \
        --broadcast \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        -vvv 2>&1 | tee /tmp/weth10-deploy.log
    
    if grep -q "WETH10 deployed at: 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f" /tmp/weth10-deploy.log; then
        echo "✅ WETH10 deployed successfully!"
    else
        echo "⚠️  WETH10 deployment may have failed or used different address"
        echo "   Check /tmp/weth10-deploy.log for details"
    fi
else
    echo "⏭️  Skipping WETH10 deployment (already exists)"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Deployment Complete"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Verifying deployments..."
WETH9_CODE_FINAL=$(cast code 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 --rpc-url "$RPC_URL" 2>/dev/null || echo "")
WETH10_CODE_FINAL=$(cast code 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f --rpc-url "$RPC_URL" 2>/dev/null || echo "")

if [ -n "$WETH9_CODE_FINAL" ] && [ "$WETH9_CODE_FINAL" != "0x" ]; then
    echo "✅ WETH9 verified at 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
else
    echo "❌ WETH9 not found at target address"
fi

if [ -n "$WETH10_CODE_FINAL" ] && [ "$WETH10_CODE_FINAL" != "0x" ]; then
    echo "✅ WETH10 verified at 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"
else
    echo "❌ WETH10 not found at target address"
fi

echo ""
echo "📋 Next Steps:"
echo "   1. Verify contract functionality"
echo "   2. Update .env with deployment details if needed"
echo ""
