#!/usr/bin/env bash
# Deploy CCIPRelayBridge (WETH) on Chain 138 and authorize on CCIP_RELAY_ROUTER_CHAIN138.
#
# Chain 138 Besu rejects Cancun bytecode (PUSH0 / 0x5f). Build + deploy with Paris EVM.
#
# Usage: ./scripts/deployment/deploy-chain138-weth-relay-bridge.sh --execute

set -euo pipefail

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

# shellcheck disable=SC1090
source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
load_deployment_env --repo-root "$PROJECT_ROOT"

EXECUTE=0
while [[ $# -gt 0 ]]; do
  case "$1" in
    --execute) EXECUTE=1; shift ;;
    --dry-run) echo "[DRY RUN] forge create CCIPRelayBridge (Paris, gas ~3.5M)"; exit 0 ;;
    *) echo "Unknown arg: $1" >&2; exit 1 ;;
  esac
done

[[ "$EXECUTE" == "1" ]] || { echo "Pass --execute"; exit 0; }
require_private_key_env || exit 1
command -v jq >/dev/null 2>&1 || { echo "jq required"; exit 1; }

RPC="${RPC_URL_138:-http://192.168.11.211:8545}"
WETH="${WETH9:-0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2}"
ROUTER="${CCIP_RELAY_ROUTER_CHAIN138:-0xe75d26bc558a28442f30750c6d97bffb46f39abc}"
GAS_LIMIT_DEPLOY="${GAS_LIMIT_DEPLOY:-3500000}"
EVM_VERSION_CHAIN138="${EVM_VERSION_CHAIN138:-paris}"

bash "$PROJECT_ROOT/scripts/forge/scope.sh" build relay --evm-version "$EVM_VERSION_CHAIN138" --force

LOG="/tmp/chain138-weth-relay-bridge.log"
out=$(bash "$PROJECT_ROOT/scripts/forge/scope.sh" create relay contracts/relay/CCIPRelayBridge.sol:CCIPRelayBridge \
  --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy \
  --evm-version "$EVM_VERSION_CHAIN138" \
  --gas-limit "$GAS_LIMIT_DEPLOY" \
  --broadcast \
  --constructor-args "$WETH" "$ROUTER" 2>&1) || {
  printf '%s\n' "$out" | tee "$LOG"
  exit 1
}
printf '%s\n' "$out" | tee "$LOG"

BRIDGE=$(printf '%s\n' "$out" | sed -n 's/.*Deployed to: \(0x[0-9a-fA-F]\{40\}\).*/\1/p' | head -1)
if [[ -z "$BRIDGE" ]]; then
  BRIDGE=$(printf '%s\n' "$out" | sed -n 's/.*"contract":"\(0x[0-9a-fA-F]\{40\}\)".*/\1/p' | head -1)
fi
if [[ -z "$BRIDGE" ]]; then
  echo "ERROR: bridge deploy failed — see $LOG" >&2
  exit 1
fi

txh=$(printf '%s\n' "$out" | sed -n 's/.*Transaction hash: \(0x[0-9a-fA-F]\{64\}\).*/\1/p' | head -1)
if [[ -n "$txh" ]]; then
  st=$(cast receipt "$txh" --json --rpc-url "$RPC" 2>/dev/null | jq -r '.status // "0x0"')
  if [[ "$st" != "0x1" ]]; then
    echo "ERROR: deploy tx $txh failed on-chain (status=$st)" >&2
    exit 1
  fi
fi

code=$(cast code "$BRIDGE" --rpc-url "$RPC" 2>/dev/null || echo "0x")
if [[ -z "$code" || "$code" == "0x" ]]; then
  echo "ERROR: no bytecode at $BRIDGE" >&2
  exit 1
fi

echo "CCIP_RELAY_BRIDGE_CHAIN138_WETH=$BRIDGE"

OK=$(cast call "$ROUTER" "authorizedBridges(address)(bool)" "$BRIDGE" --rpc-url "$RPC" 2>/dev/null || echo "false")
if [[ "$OK" != "true" ]]; then
  echo "=== Authorize bridge on relay router ==="
  cast send "$ROUTER" "authorizeBridge(address)" "$BRIDGE" \
    --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-price 5000000000
fi

echo "Done."
