#!/usr/bin/env bash
# ensure-runner-deps.sh — idempotent install of runner dependencies.
#
# Source checkout runs TypeScript via Node/tsx and imports @farmslot/recipe-harness.
# Cloned runner checkouts under product temp/ often have no node_modules, so use
# the runner's pinned Yarn release and immutable lockfile.
#
# Usage: ensure-runner-deps.sh [RUNNER_DIR]
# Env:   FARMSLOT_ROOT / METAMASK_RUNNER_PROTOCOL_ROOT — optional local link target
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUNNER_DIR="${1:-}"
if [ -z "$RUNNER_DIR" ]; then
  RUNNER_DIR="$(cd "$SCRIPT_DIR/../.." && pwd -P)"
fi
RUNNER_DIR="$(cd "$RUNNER_DIR" && pwd -P)"

PROTOCOL_ROOT="${METAMASK_RUNNER_PROTOCOL_ROOT:-${METAMASK_RUNNER_FARMSLOT_ROOT:-${FARMSLOT_ROOT:-}}}"
if [ -z "$PROTOCOL_ROOT" ] && [ -f "$RUNNER_DIR/.farmslot-root" ]; then
  PROTOCOL_ROOT="$(cat "$RUNNER_DIR/.farmslot-root")"
fi

deps_ready() {
  node "$SCRIPT_DIR/runner-deps-ready.mjs" \
    "$RUNNER_DIR" "$PROTOCOL_ROOT"
}

# Library actions import @deeeed/metamask-harness by package name; a source checkout
# is not npm-installed into its own node_modules, so symlink self for resolution.
ensure_self_package_link() {
  local link="$RUNNER_DIR/node_modules/@deeeed/metamask-harness"
  /bin/mkdir -p "$RUNNER_DIR/node_modules/@deeeed"
  if [ ! -e "$link" ]; then
    ln -sf "$RUNNER_DIR" "$link"
  fi
}

ensure_dev_package_condition() {
  if [ -f "$RUNNER_DIR/dist/index.js" ]; then
    return 0
  fi
  if [ ! -f "$RUNNER_DIR/src/index.ts" ]; then
    return 0
  fi
  case " ${NODE_OPTIONS:-} " in
    *" --conditions=development "*) return 0 ;;
  esac
  export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--conditions=development"
}

if deps_ready; then
  ensure_self_package_link
  ensure_dev_package_condition
  exit 0
fi

if [ ! -f "$RUNNER_DIR/package.json" ]; then
  echo "mm-harness: runner package.json missing: $RUNNER_DIR" >&2
  exit 1
fi

if [ -n "$PROTOCOL_ROOT" ] && [ -f "$RUNNER_DIR/scripts/link-local-farmslot.mjs" ]; then
  if [ -f "$PROTOCOL_ROOT/packages/recipe-harness/package.json" ]; then
    FARMSLOT_ROOT="$(cd "$PROTOCOL_ROOT" && pwd -P)" \
      node "$RUNNER_DIR/scripts/link-local-farmslot.mjs" >&2 || true
    if deps_ready; then
      ensure_self_package_link
      ensure_dev_package_condition
      exit 0
    fi
  fi
fi

echo "mm-harness: installing runner dependencies via Yarn in $RUNNER_DIR" >&2
(
  cd "$RUNNER_DIR"
  umask 022
  COREPACK_BIN="$(type -P corepack || true)"
  if [ -z "$COREPACK_BIN" ]; then
    echo "mm-harness: Corepack is required to install runner dependencies" >&2
    echo "Next: npm install -g corepack && corepack enable" >&2
    exit 1
  fi
  YARN_RC_FILENAME=.mm-harness-runner.yml \
    YARN_NODE_LINKER=node-modules \
    YARN_ENABLE_GLOBAL_CACHE=false \
    YARN_CACHE_FOLDER="$RUNNER_DIR/.yarn/cache" \
    "$COREPACK_BIN" yarn install --immutable --mode=skip-build >&2
)

# Yarn may replace the local protocol links with registry packages. Restore the
# explicit co-development override after the complete dependency tree exists.
if [ -n "$PROTOCOL_ROOT" ] && [ -f "$RUNNER_DIR/scripts/link-local-farmslot.mjs" ]; then
  FARMSLOT_ROOT="$PROTOCOL_ROOT" node "$RUNNER_DIR/scripts/link-local-farmslot.mjs" >&2
fi

if ! deps_ready; then
  echo "mm-harness: runner dependencies still missing after Yarn install: $RUNNER_DIR" >&2
  exit 1
fi

ensure_self_package_link
ensure_dev_package_condition
