.PHONY: install frontend dev serve test lint bench docs build docker docker-up docker-test clean help

# A workspace outside the repository, so the explorer and file pickers show your
# graphs and data rather than the source tree (as in the devcontainer).
WORKSPACE ?= $(HOME)/noodlelab-workspace

install:            ## Python env (every extra, with the dev tools) + frontend deps
	uv sync --all-extras
	cd frontend && npm ci

frontend:           ## Build the editor into src/noodlelab/static
	cd frontend && npm run build

serve:              ## Run the server (serves the built editor)
	mkdir -p $(WORKSPACE) && uv run noodlelab serve $(WORKSPACE)

dev:                ## API on :8000 plus Vite with hot reload on :5173
	mkdir -p $(WORKSPACE) && uv run noodlelab serve $(WORKSPACE) --no-browser & cd frontend && npm run dev

test:               ## Run the tests (parallel; pytest -n0 for serial)
	uv run pytest -q

lint:               ## ruff check, ruff format --check and tsc
	uv run ruff check . && uv run ruff format --check .
	cd frontend && npm run typecheck

bench:              ## Time the hot paths (benchmarks/bench.py --help: save and compare runs)
	uv run python benchmarks/bench.py

docs:               ## The documentation site in docs/_build/html (needs: uv sync --all-extras --group docs)
	uv run sphinx-build -W --keep-going -b html docs docs/_build/html

build: frontend     ## Wheel + sdist with the editor included
	uv build

# The version as hatch-vcs gives it: v0.1.1 builds 0.1.1, later commits 0.1.2.devN+g<sha>.
VERSION = $(shell uvx --quiet --from setuptools-scm python -m setuptools_scm 2>/dev/null || echo 0.0.0)

docker:             ## Build the image, versioned from the git tag
	docker build -t noodlelab --build-arg NOODLELAB_VERSION=$(VERSION) .

docker-up:          ## Build and start docker-compose.yml, versioned from the git tag
	NOODLELAB_VERSION=$(VERSION) docker compose up -d --build

docker-test:        ## tsc + vite build + pytest inside a container with Python and Node
	docker build -f Dockerfile.test -t noodlelab:test .
	docker run --rm noodlelab:test

clean:              ## Remove build output and caches
	rm -rf dist docs/_build docs/reference/_generated .pytest_cache .ruff_cache src/noodlelab/static/assets src/noodlelab/static/index.html src/noodlelab/static/admin.html src/noodlelab/static/calc.html

help:               ## List the targets
	@grep -E '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | sed -E 's/:[^#]*## /\t/'
