SHELL := /bin/bash

.PHONY: serve build-web build-web-dev sync-viewer sync-wasm sync-samples run download-mp test

# Pinned MicroPython WASM runtime (official @micropython npm package, PyScript variant).
# sha256 from micropython.mjs / micropython.wasm of the published package.
MP_NPM          := @micropython/micropython-webassembly-pyscript@1.28.0-6
MP_DIR          := public/mp
MP_MJS_SHA256   := 8c4067836ce5b71aeddac0315c25f73b642399fdb2e2e94acea612f60747b21a
MP_WASM_SHA256  := 3f705482c97498cb24c90275b33038d9a2d1868439d8216002d90a75bdd28997

# Fetch the MicroPython WASM runtime from jsDelivr unless the pinned files are
# already present and match the known-good hashes. Served from public/mp/.
download-mp:
	@mkdir -p $(MP_DIR)
	@if echo "$(MP_MJS_SHA256)  $(MP_DIR)/micropython.mjs" | sha256sum -c - --quiet 2>/dev/null \
	  && echo "$(MP_WASM_SHA256)  $(MP_DIR)/micropython.wasm" | sha256sum -c - --quiet 2>/dev/null; then \
	  echo "==> micropython $(MP_NPM): up to date"; \
	else \
	  echo "==> downloading micropython $(MP_NPM)"; \
	  curl -fL -o $(MP_DIR)/micropython.mjs  https://cdn.jsdelivr.net/npm/$(MP_NPM)/micropython.mjs; \
	  curl -fL -o $(MP_DIR)/micropython.wasm https://cdn.jsdelivr.net/npm/$(MP_NPM)/micropython.wasm; \
	  echo "$(MP_MJS_SHA256)  $(MP_DIR)/micropython.mjs"  | sha256sum -c -; \
	  echo "$(MP_WASM_SHA256)  $(MP_DIR)/micropython.wasm" | sha256sum -c -; \
	fi

# The engine pkg — whole-source `compile()` only (bimr-wasm, D8). Built by
# `make -C ../bimr-wasm pkg` (the factory `build-wasm` target once renamed).
sync-wasm:
	@mkdir -p src/wasm
	@test -f ../bimr-wasm/pkg/bimr_wasm.js || \
	  (echo "==> bimr-wasm pkg missing — run 'make -C ../bimr-wasm pkg'" >&2; exit 1)
	@cp ../bimr-wasm/pkg/bimr_wasm.js ../bimr-wasm/pkg/bimr_wasm_bg.wasm \
	   ../bimr-wasm/pkg/bimr_wasm_bg.wasm.d.ts ../bimr-wasm/pkg/bimr_wasm.d.ts src/wasm/
	@echo "==> synced bimr-wasm pkg into src/wasm"

# Canonical Python samples live in bimr-engine/samples/python/.
# The corpus is the engine's supported subset (wall.py + pure-subset
# samples); the operator corpus stayed in bimr-black (archived).
ENGINE_SAMPLES := ../bimr-engine/samples/python
BIMR_SAMPLES   := building.py grid.py cylinders.py

sync-samples:
	@mkdir -p public/samples
	@rm -f public/samples/*.py
	@for s in $(BIMR_SAMPLES); do \
	  test -f $(ENGINE_SAMPLES)/$$s || (echo "==> missing sample $$s" >&2; exit 1); \
	  cp $(ENGINE_SAMPLES)/$$s public/samples/; \
	done
	@echo "==> synced samples from bimr-engine"

# Vite HMR dev server — base=/editor/ matches the production deployment path
serve: download-mp sync-wasm sync-samples
	pnpm vite dev --base=/editor/

build-web: sync-viewer download-mp sync-wasm sync-samples
	pnpm build

# Build for embedding at /editor/ (dev.bimr.net — served under /editor/ path).
build-web-dev: sync-viewer download-mp sync-wasm sync-samples
	pnpm vite build --base=/editor/

# Build bimr-viewer-ifcx/src/viewer/render.ts twice:
#  - standalone artifact keeps the CDN URLs (no plugin);
#  - app bundle gets bare imports via the cdn-to-bare plugin — written into
#    src/ so Vite resolves three from node_modules at build time (the app
#    has no runtime CDN dependency).
sync-viewer:
	@mkdir -p viewer ../bimr-viewer-ifcx/web/viewer
	@test -x ../bimr-viewer-ifcx/src/node_modules/.bin/esbuild || \
	  (echo "==> esbuild missing in bimr-viewer-ifcx/src — npm install (first run only)" && \
	   cd ../bimr-viewer-ifcx/src && npm install)
	cd ../bimr-viewer-ifcx/src && node_modules/.bin/esbuild viewer/render.ts --bundle --outfile=../web/viewer/render.mjs --external:three --format=esm
	cd ../bimr-viewer-ifcx/src && node ../../bimr-web/scripts/bundle-app.mjs

# Playwright app tests (build first: make build-web). Installs the pinned
# chromium build on first use (idempotent afterwards).
test:
	pnpm exec playwright install chromium && \
	pnpm exec playwright test
