# A local venv. Homebrew Python refuses `pip install --user` (PEP 668), so the
# venv is not a preference: it is what works on macOS without touching the system.
VENV := .venv
PY   := $(VENV)/bin/python
PIP  := $(VENV)/bin/pip
ROOT := $(shell pwd)

.PHONY: help install registry seed test typecheck build dev api web up down matrix oracle clean distclean

help:
	@echo "make install    install the Python and Node dependencies"
	@echo "make registry   rebuild the English registry from the .pt.json"
	@echo "make seed       regenerate the seed (only after a new crawl)"
	@echo "make test       run the suite (domain + API + traceability)"
	@echo "make typecheck  tsc --noEmit on the frontend"
	@echo "make build      production build of the frontend"
	@echo "make api        start the gateway on :8080"
	@echo "make web        start the frontend on :5173"
	@echo "make dev        start both"
	@echo "make up         Compose: 10 services + Postgres + RabbitMQ + MinIO"
	@echo "make matrix     generate the traceability matrix (Phase 5)"
	@echo "make oracle     run the equivalence oracle against the COMPUTED cells"

$(VENV):
	python3 -m venv $(VENV)
	$(PIP) install --upgrade pip

install: $(VENV)
	$(PIP) install fastapi "uvicorn[standard]" httpx pytest
	cd frontend && npm install
	@echo
	@echo "ready. now:  make test"

registry:
	$(PY) tools/build_registry.py

seed:
	SPREADSHEET_DATA=$${SPREADSHEET_DATA:-../spreadsheet_data} $(PY) tools/build_seed.py

test: | $(VENV)
	$(PY) -m pytest

typecheck:
	cd frontend && npx tsc --noEmit

build:
	cd frontend && npx vite build

api: | $(VENV)
	cd services/gateway && $(ROOT)/$(PY) -m uvicorn main:app --port 8080 --reload

web:
	cd frontend && npx vite

dev: | $(VENV)
	@( cd services/gateway && $(ROOT)/$(PY) -m uvicorn main:app --port 8080 & ) ; \
	 cd frontend && npx vite

up:
	docker compose up --build

down:
	docker compose down -v

matrix:
	$(PY) tools/generate_traceability_matrix.py
	$(PY) tools/render_traceability_md.py

oracle:
	$(PY) tools/equivalence_oracle.py

clean:
	find . -name __pycache__ -type d -exec rm -rf {} + 2>/dev/null || true
	rm -rf frontend/dist .pytest_cache

distclean: clean
	rm -rf $(VENV) frontend/node_modules
