#!/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:-}"
PRIVATE_KEY="${PRIVATE_KEY:-}"
rpc_url_override=false
private_key_override=false
CLI_RPC_URL=""
CLI_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"
            CLI_RPC_URL="$2"
            rpc_url_override=true
            shift 2
            ;;
        --private-key)
            PRIVATE_KEY="$2"
            CLI_PRIVATE_KEY="$2"
            private_key_override=true
            shift 2
            ;;
        --help)
            usage
            ;;
        *)
            echo "ERROR: Unknown option: $1" >&2
            usage
            ;;
    esac
done

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

if [[ "$rpc_url_override" == true ]]; then
    RPC_URL="$CLI_RPC_URL"
else
    RPC_URL="${RPC_URL:-${RPC_URL_138:-${CHAIN138_RPC_URL:-http://localhost:8545}}}"
fi

if [[ "$private_key_override" == true ]]; then
    export PRIVATE_KEY="$CLI_PRIVATE_KEY"
fi

if ! require_private_key_env; then
    exit 1
fi

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" >&2
        echo "Valid methods: create, create2, genesis" >&2
        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!"
