#!/usr/bin/env bash
# Unified WETH Deployment Script
# Supports multiple deployment methods: create, create2, genesis, and bridge deployment

set -e

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

# Default values
METHOD="${METHOD:-create}"
TOKEN="${TOKEN:-both}"  # weth9, weth10, or both
DEPLOY_BRIDGE="${DEPLOY_BRIDGE:-false}"
RPC_URL="${RPC_URL:-http://localhost:8545}"
PRIVATE_KEY="${PRIVATE_KEY:-}"

usage() {
    cat <<EOF
Usage: $0 [OPTIONS]

Deploy WETH contracts using various methods.

Options:
    --method METHOD      Deployment method: create, create2, or genesis (default: create)
    --token TOKEN        Token to deploy: weth9, weth10, or both (default: both)
    --bridge             Also deploy CCIP bridges (default: false)
    --rpc-url URL        RPC endpoint URL (default: http://localhost:8545)
    --private-key KEY    Deployer private key
    --help               Show this help message

Examples:
    # Deploy both WETH9 and WETH10 using CREATE
    $0 --method create --token both

    # Deploy WETH9 only using CREATE2
    $0 --method create2 --token weth9

    # Deploy with bridges
    $0 --method create --token both --bridge
EOF
    exit 0
}

# Parse arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        --method)
            METHOD="$2"
            shift 2
            ;;
        --token)
            TOKEN="$2"
            shift 2
            ;;
        --bridge)
            DEPLOY_BRIDGE=true
            shift
            ;;
        --rpc-url)
            RPC_URL="$2"
            shift 2
            ;;
        --private-key)
            PRIVATE_KEY="$2"
            shift 2
            ;;
        --help)
            usage
            ;;
        *)
            echo "Unknown option: $1"
            usage
            ;;
    esac
done

if [ -z "$PRIVATE_KEY" ]; then
    echo "Error: PRIVATE_KEY environment variable not set"
    exit 1
fi

cd "$PROJECT_ROOT"

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🚀 Unified WETH Deployment"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Method: $METHOD"
echo "Token: $TOKEN"
echo "Deploy Bridge: $DEPLOY_BRIDGE"
echo "RPC URL: $RPC_URL"
echo ""

# Route to appropriate deployment script based on method
case "$METHOD" in
    create)
        if [ "$TOKEN" = "weth9" ] || [ "$TOKEN" = "both" ]; then
            echo "Deploying WETH9..."
            forge script script/DeployWETH.s.sol \
                --rpc-url "$RPC_URL" \
                --broadcast \
                --private-key "$PRIVATE_KEY" \
                --sig "run()" || true
        fi
        if [ "$TOKEN" = "weth10" ] || [ "$TOKEN" = "both" ]; then
            echo "Deploying WETH10..."
            forge script script/DeployWETH10.s.sol \
                --rpc-url "$RPC_URL" \
                --broadcast \
                --private-key "$PRIVATE_KEY" \
                --sig "run()" || true
        fi
        ;;
    create2)
        "$SCRIPT_DIR/deploy-weth-create2.sh" --token "$TOKEN"
        ;;
    genesis)
        echo "Genesis deployment should be done during genesis generation"
        echo "Use: scripts/genesis/add-weth-to-genesis.sh"
        exit 1
        ;;
    *)
        echo "Error: Unknown method: $METHOD"
        echo "Valid methods: create, create2, genesis"
        exit 1
        ;;
esac

if [ "$DEPLOY_BRIDGE" = "true" ]; then
    echo ""
    echo "Deploying CCIP bridges..."
    if [ "$TOKEN" = "weth9" ] || [ "$TOKEN" = "both" ]; then
        "$SCRIPT_DIR/deploy-ccip-weth9-bridge.sh" || true
    fi
    if [ "$TOKEN" = "weth10" ] || [ "$TOKEN" = "both" ]; then
        "$SCRIPT_DIR/deploy-ccip-weth10-bridge.sh" || true
    fi
fi

echo ""
echo "✅ Deployment complete!"
