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

ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
CHROMIUM="${CHROMIUM:-$(command -v chromium || command -v chromium-browser || command -v google-chrome || command -v google-chrome-stable || true)}"
BOUNDARY_PROOF_GATES="$ROOT_DIR/build/wasm/sim-configs-inventory/boundary-proof-gates.tsv"
BOUNDARY_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/boundary-summary.tsv"
INI_BOUNDARY_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/ini-boundary-summary.tsv"
NATIVE_PROOF_ALIGNMENT_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/native-proof-alignment-summary.tsv"
NATIVE_RUNTIME_PROBE_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/native-runtime-probe-summary.tsv"
RUNTIME_BOUNDARY_NATIVE_ALIGNMENT_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-native-alignment-summary.tsv"
RUNTIME_PROBE_GATE_ALIGNMENT="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-probe-gate-alignment.tsv"
NATIVE_RUNTIME_PROBE_EXECUTION_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/native-runtime-probe-execution-plan.tsv"
NATIVE_RUNTIME_PROBE_PASS_EVIDENCE_CONTRACT="$ROOT_DIR/build/wasm/sim-configs-inventory/native-runtime-probe-pass-evidence-contract.tsv"
RUNTIME_BOUNDARY_CONTRACT_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-contract-summary.tsv"
USER_M_PROCESS_NATIVE_STATE_ALIGNMENT="$ROOT_DIR/build/wasm/sim-configs-inventory/user-m-process-native-state-alignment.tsv"
USER_M_PROCESS_NATIVE_TRANSITION_ALIGNMENT="$ROOT_DIR/build/wasm/sim-configs-inventory/user-m-process-native-transition-alignment.tsv"
USER_M_PROCESS_NATIVE_RUNTIME_STATE_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/user-m-process-native-runtime-state-plan.tsv"
USER_M_PROCESS_NATIVE_RUNTIME_READINESS="$ROOT_DIR/build/wasm/sim-configs-inventory/user-m-process-native-runtime-readiness.tsv"
USER_M_PROCESS_NATIVE_RUNTIME_PROBE_GATE="$ROOT_DIR/build/wasm/sim-configs-inventory/user-m-process-native-runtime-probe-gate.tsv"
USER_M_PROCESS_TRANSITION_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/user-m-process-transition-plan.tsv"
TOOL_DB_PROCESS_NATIVE_PROTOCOL_ALIGNMENT="$ROOT_DIR/build/wasm/sim-configs-inventory/tool-db-process-native-protocol-alignment.tsv"
TOOL_DB_PROCESS_NATIVE_RUNTIME_READINESS="$ROOT_DIR/build/wasm/sim-configs-inventory/tool-db-process-native-runtime-readiness.tsv"
TOOL_DB_PROCESS_NATIVE_RUNTIME_PROBE_GATE="$ROOT_DIR/build/wasm/sim-configs-inventory/tool-db-process-native-runtime-probe-gate.tsv"
TOOL_DB_PROCESS_TRANSACTION_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/tool-db-process-transaction-plan.tsv"
PYTHON_REMAP_NATIVE_RUNTIME_ALIGNMENT="$ROOT_DIR/build/wasm/sim-configs-inventory/python-remap-native-runtime-alignment.tsv"
PYTHON_REMAP_NATIVE_RUNTIME_READINESS="$ROOT_DIR/build/wasm/sim-configs-inventory/python-remap-native-runtime-readiness.tsv"
PYTHON_REMAP_NATIVE_RUNTIME_PROBE_GATE="$ROOT_DIR/build/wasm/sim-configs-inventory/python-remap-native-runtime-probe-gate.tsv"
PYTHON_REMAP_NATIVE_RUNTIME_STATE_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/python-remap-native-runtime-state-plan.tsv"
PYTHON_REMAP_NATIVE_RUNTIME_FIXTURE_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/python-remap-native-runtime-fixture-plan.tsv"
PYTHON_REMAP_RUNTIME_CONTRACT="$ROOT_DIR/build/wasm/sim-configs-inventory/python-remap-runtime-contract.tsv"
BLOCKED_RUNTIME_PROMOTION_LOCK="$ROOT_DIR/build/wasm/sim-configs-inventory/blocked-runtime-promotion-lock.tsv"
RUNTIME_BOUNDARY_PROMOTION_READINESS="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-promotion-readiness.tsv"
RUNTIME_BOUNDARY_PROMOTION_BLOCKERS="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-promotion-blockers.tsv"
RUNTIME_BOUNDARY_POST_NATIVE_PASS_GATES="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-post-native-pass-gates.tsv"
RUNTIME_BOUNDARY_HOST_PREFLIGHT="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-host-preflight.tsv"
RUNTIME_BOUNDARY_HOST_REQUIREMENT_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-host-requirement-summary.tsv"
RUNTIME_BOUNDARY_HOST_UNBLOCK_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-host-unblock-plan.tsv"
RUNTIME_BOUNDARY_FAMILY_HOST_READINESS="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-family-host-readiness.tsv"
RUNTIME_BOUNDARY_HOST_READINESS_ROLLUP="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-host-readiness-rollup.tsv"
RUNTIME_BOUNDARY_OPT_IN_PROBE_DISPATCH_PLAN="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-opt-in-probe-dispatch-plan.tsv"
RUNTIME_BOUNDARY_OPT_IN_PROBE_DISPATCH_ROLLUP="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-opt-in-probe-dispatch-rollup.tsv"
RUNTIME_BOUNDARY_OPT_IN_PROBE_SKIP_EVIDENCE_CONTRACT="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-opt-in-probe-skip-evidence-contract.tsv"
RUNTIME_BOUNDARY_OPT_IN_PROBE_SKIP_EVIDENCE_ROLLUP="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-opt-in-probe-skip-evidence-rollup.tsv"
RUNTIME_BOUNDARY_NATIVE_EVIDENCE_ACCEPTANCE_GATE="$ROOT_DIR/build/wasm/sim-configs-inventory/runtime-boundary-native-evidence-acceptance-gate.tsv"
BOUNDARY_PHASE_COMPLETION_SUMMARY="$ROOT_DIR/build/wasm/sim-configs-inventory/boundary-phase-completion-summary.tsv"
NEXT_BOUNDARY_WORKLIST="$ROOT_DIR/build/wasm/sim-configs-inventory/next-boundary-worklist.tsv"
NEXT_BOUNDARY_RECOMMENDATIONS="$ROOT_DIR/build/wasm/sim-configs-inventory/next-boundary-recommendations.tsv"
INVENTORY_SCRIPT="$ROOT_DIR/tests/wasm/node/verify_sim_configs_inventory_wasm.mjs"
NATIVE_SOURCE_PROOF_SUMMARY="$ROOT_DIR/build/native/native-source-proof-summary.tsv"
NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY="$ROOT_DIR/build/native/native-runtime-probe-summary.tsv"

if [[ -z "$CHROMIUM" ]]; then
  echo "missing Chromium-compatible browser; set CHROMIUM=/path/to/browser" >&2
  exit 1
fi

if [[ "${SKIP_INTERP_BUILD:-0}" != "1" ]]; then
  "$ROOT_DIR/tools/build_wasm_core.sh"
fi
if [[ "${SKIP_INI_BUILD:-0}" != "1" ]]; then
  "$ROOT_DIR/tools/build_ini_panel.sh"
fi
INVENTORY_REQUIRED=0
for artifact in \
  "$BOUNDARY_PROOF_GATES" \
  "$BOUNDARY_SUMMARY" \
  "$INI_BOUNDARY_SUMMARY" \
  "$NATIVE_PROOF_ALIGNMENT_SUMMARY" \
  "$RUNTIME_BOUNDARY_NATIVE_ALIGNMENT_SUMMARY" \
  "$RUNTIME_PROBE_GATE_ALIGNMENT" \
  "$NATIVE_RUNTIME_PROBE_EXECUTION_PLAN" \
  "$NATIVE_RUNTIME_PROBE_PASS_EVIDENCE_CONTRACT" \
  "$RUNTIME_BOUNDARY_CONTRACT_SUMMARY" \
  "$USER_M_PROCESS_NATIVE_STATE_ALIGNMENT" \
  "$USER_M_PROCESS_NATIVE_TRANSITION_ALIGNMENT" \
  "$USER_M_PROCESS_NATIVE_RUNTIME_STATE_PLAN" \
  "$USER_M_PROCESS_NATIVE_RUNTIME_READINESS" \
  "$USER_M_PROCESS_NATIVE_RUNTIME_PROBE_GATE" \
  "$USER_M_PROCESS_TRANSITION_PLAN" \
  "$TOOL_DB_PROCESS_NATIVE_PROTOCOL_ALIGNMENT" \
  "$TOOL_DB_PROCESS_NATIVE_RUNTIME_READINESS" \
  "$TOOL_DB_PROCESS_NATIVE_RUNTIME_PROBE_GATE" \
  "$TOOL_DB_PROCESS_TRANSACTION_PLAN" \
  "$PYTHON_REMAP_NATIVE_RUNTIME_ALIGNMENT" \
  "$PYTHON_REMAP_NATIVE_RUNTIME_READINESS" \
  "$PYTHON_REMAP_NATIVE_RUNTIME_PROBE_GATE" \
  "$PYTHON_REMAP_NATIVE_RUNTIME_STATE_PLAN" \
  "$PYTHON_REMAP_NATIVE_RUNTIME_FIXTURE_PLAN" \
  "$PYTHON_REMAP_RUNTIME_CONTRACT" \
  "$NATIVE_RUNTIME_PROBE_SUMMARY" \
  "$BLOCKED_RUNTIME_PROMOTION_LOCK" \
  "$RUNTIME_BOUNDARY_PROMOTION_READINESS" \
  "$RUNTIME_BOUNDARY_PROMOTION_BLOCKERS" \
  "$RUNTIME_BOUNDARY_POST_NATIVE_PASS_GATES" \
  "$RUNTIME_BOUNDARY_HOST_PREFLIGHT" \
  "$RUNTIME_BOUNDARY_HOST_REQUIREMENT_SUMMARY" \
  "$RUNTIME_BOUNDARY_HOST_UNBLOCK_PLAN" \
  "$RUNTIME_BOUNDARY_FAMILY_HOST_READINESS" \
  "$RUNTIME_BOUNDARY_HOST_READINESS_ROLLUP" \
  "$RUNTIME_BOUNDARY_OPT_IN_PROBE_DISPATCH_PLAN" \
  "$RUNTIME_BOUNDARY_OPT_IN_PROBE_DISPATCH_ROLLUP" \
  "$RUNTIME_BOUNDARY_OPT_IN_PROBE_SKIP_EVIDENCE_CONTRACT" \
  "$RUNTIME_BOUNDARY_OPT_IN_PROBE_SKIP_EVIDENCE_ROLLUP" \
  "$RUNTIME_BOUNDARY_NATIVE_EVIDENCE_ACCEPTANCE_GATE" \
  "$BOUNDARY_PHASE_COMPLETION_SUMMARY" \
  "$NEXT_BOUNDARY_WORKLIST" \
  "$NEXT_BOUNDARY_RECOMMENDATIONS"; do
  if [[ ! -f "$artifact" || "$INVENTORY_SCRIPT" -nt "$artifact" ]]; then
    INVENTORY_REQUIRED=1
  fi
done
if [[ -f "$NATIVE_SOURCE_PROOF_SUMMARY" && "$NATIVE_SOURCE_PROOF_SUMMARY" -nt "$NATIVE_PROOF_ALIGNMENT_SUMMARY" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$NATIVE_RUNTIME_PROBE_SUMMARY" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_PROBE_GATE_ALIGNMENT" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$NATIVE_RUNTIME_PROBE_EXECUTION_PLAN" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$NATIVE_RUNTIME_PROBE_PASS_EVIDENCE_CONTRACT" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_PROMOTION_READINESS" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_PROMOTION_BLOCKERS" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_POST_NATIVE_PASS_GATES" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_HOST_PREFLIGHT" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_HOST_REQUIREMENT_SUMMARY" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_HOST_UNBLOCK_PLAN" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_FAMILY_HOST_READINESS" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_HOST_READINESS_ROLLUP" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_OPT_IN_PROBE_DISPATCH_PLAN" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_OPT_IN_PROBE_DISPATCH_ROLLUP" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_OPT_IN_PROBE_SKIP_EVIDENCE_CONTRACT" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_OPT_IN_PROBE_SKIP_EVIDENCE_ROLLUP" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ -f "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" && "$NATIVE_RUNTIME_PROBE_SOURCE_SUMMARY" -nt "$RUNTIME_BOUNDARY_NATIVE_EVIDENCE_ACCEPTANCE_GATE" ]]; then
  INVENTORY_REQUIRED=1
fi
if [[ "$INVENTORY_REQUIRED" == "1" ]]; then
  SKIP_INTERP_BUILD=1 "$ROOT_DIR/tests/wasm/node/verify_sim_configs_inventory_wasm.sh"
fi

TMP_DIR="$(mktemp -d)"
PORT_FILE="$TMP_DIR/port"
SERVER_LOG="$TMP_DIR/server.log"
CHROME_PROFILE="$TMP_DIR/chrome-profile"
mkdir -p "$CHROME_PROFILE"

cleanup() {
  if [[ -n "${SERVER_PID:-}" ]]; then
    kill "$SERVER_PID" 2>/dev/null || true
    wait "$SERVER_PID" 2>/dev/null || true
  fi
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

python3 - <<'PY' "$ROOT_DIR" "$PORT_FILE" >"$SERVER_LOG" 2>&1 &
import functools
import http.server
import pathlib
import socketserver
import sys

root = pathlib.Path(sys.argv[1])
port_file = pathlib.Path(sys.argv[2])

handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=str(root))
with socketserver.TCPServer(("127.0.0.1", 0), handler) as httpd:
    port_file.write_text(str(httpd.server_address[1]), encoding="ascii")
    httpd.serve_forever()
PY
SERVER_PID=$!

for _ in $(seq 1 100); do
  [[ -s "$PORT_FILE" ]] && break
  sleep 0.05
done

if [[ ! -s "$PORT_FILE" ]]; then
  echo "browser interpreter smoke HTTP server did not start" >&2
  cat "$SERVER_LOG" >&2 || true
  exit 1
fi

PORT="$(cat "$PORT_FILE")"
URL="http://127.0.0.1:$PORT/tests/browser/interp_smoke.html"
OUT="$TMP_DIR/chromium.out"

"$CHROMIUM" \
  --headless=new \
  --disable-gpu \
  --no-sandbox \
  --user-data-dir="$CHROME_PROFILE" \
  --virtual-time-budget=10000 \
  --dump-dom \
  "$URL" >"$OUT" 2>&1

if ! grep -Fq "browser_interp_smoke=ok" "$OUT"; then
  echo "browser interpreter smoke failed" >&2
  sed -n '1,220p' "$OUT" >&2
  exit 1
fi

echo "browser_interp_smoke=ok"
