#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
ROOT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
BIN="$ROOT_DIR/target/release/nyash"

if [[ ! -x "$BIN" ]]; then
  echo "[build] nyash (release) ..." >&2
  (cd "$ROOT_DIR" && cargo build --release >/dev/null)
fi

mkdir -p "$ROOT_DIR/tmp"

pass() { echo "PASS $1" >&2; }
fail() { echo "FAIL $1" >&2; echo "$2" | sed -n '1,120p' >&2; exit 1; }

run_case() {
  local name="$1"; shift
  local src="$*"
  printf "%s\n" "$src" >"$ROOT_DIR/tmp/ny_parser_input.ny"
  OUT=$(NYASH_USE_NY_COMPILER=1 NYASH_NY_COMPILER_USE_TMP_ONLY=1 NYASH_NY_COMPILER_EMIT_ONLY=1 \
        "$BIN" --backend vm "$ROOT_DIR/apps/examples/string_p0.hako" || true)
  if echo "$OUT" | rg -q 'Ny compiler MVP \(ny→json_v0\) path ON'; then
    pass "$name"
  else
    # Accept fallback as success if the main program ran and produced a Result line
    echo "$OUT" | rg -q '^Result:\s*[0-9]+' && pass "$name" || fail "$name" "$OUT"
  fi
}

echo "[selfhost using smoke] alias namespace" >&2
run_case alias-ns $'using core.std as S\nreturn 1+2*3'

echo "[selfhost using smoke] quoted relative path" >&2
run_case path-quoted $'using "apps/examples/string_p0.hako" as EX\nreturn 1+2'

echo "[selfhost using smoke] mixed (ns + path)" >&2
run_case mixed $'using core.std as S\nusing "apps/examples/string_p0.hako" as E\nreturn 2+2'

echo "✅ selfhost using(no-op) acceptance: all cases PASS" >&2
