# with.bowden.works — CI entrypoint + dev helpers.
# `make check` is the gate every session runs before calling work done.
# Host Python is 3.12 with no system pip installs — all host-side
# tooling lives in ./.venv; the container installs its own deps from
# the requirements files.

VENV := .venv
BIN  := $(VENV)/bin

.PHONY: check check-backend check-frontend venv dev-backend dev-frontend extract deploy e2e

check: check-backend check-frontend

# Backend: lint + tests via the host venv.
check-backend: venv
	$(BIN)/ruff check .
	$(BIN)/pytest

# Frontend: install once, then types + unit tests.
check-frontend:
	@test -d frontend/node_modules || npm ci --prefix frontend
	cd frontend && npx tsc -b
	cd frontend && npx vitest run

# Host-side tooling venv. Idempotent: creates it if absent, ensures the
# check tooling is present, then syncs the app requirements so pytest
# can import the app.
venv:
	@test -x $(BIN)/python || python3 -m venv $(VENV)
	@test -x $(BIN)/pytest && test -x $(BIN)/ruff || \
		$(BIN)/pip install --quiet pytest ruff httpx fastapi
	@if [ -f requirements.txt ]; then $(BIN)/pip install --quiet -r requirements.txt; fi
	@if [ -f requirements-dev.txt ]; then $(BIN)/pip install --quiet -r requirements-dev.txt; fi

# Run the API locally with reload (http://localhost:8000).
dev-backend: venv
	$(BIN)/uvicorn app.main:app --reload

# Vite dev server (proxies /api to the local backend).
dev-frontend:
	@test -d frontend/node_modules || npm ci --prefix frontend
	npm run dev --prefix frontend

# Diff-harness old side: extract canonical JSON from live Supabase
# (reads .supabase-export.env; writes to stdout unless --out).
extract: venv
	$(BIN)/python -m harness.extract_legacy --env-file .supabase-export.env

# Legacy -> R1 transforms against the sidecar (host-side; reads .env).
migrate: venv
	$(BIN)/python -m scripts.migrate_legacy

deploy:
	srv-gw deploy --project with --build

# Playwright e2e smoke suite (M8). DELIBERATELY NOT part of `make check`:
# it needs a Chromium binary (`npx playwright install chromium`, one-time)
# AND a dedicated migrated + seeded e2e Postgres reachable via WITH_DB_URL —
# neither exists in CI, so the default gate must stay hermetic. See docs/E2E.md.
#
# @playwright/test is installed here with `--no-save` so it never enters
# frontend/package.json or the lockfile (keeping `npm ci` / the Docker build
# hermetic — playwright is a dev-only tool, not a shipped SPA dependency).
# Prereqs handled here: frontend deps + a fresh SPA build (the FastAPI
# webServer serves frontend/dist; without it the app is API-only and every
# spec fails at the first navigation). The venv target provisions uvicorn.
# Playwright boots the app itself (see webServer in playwright.config.ts).
e2e: venv
	@test -d frontend/node_modules || npm ci --prefix frontend
	npm install --no-save --prefix frontend @playwright/test@^1.48.0
	npm run build --prefix frontend
	cd frontend && NODE_PATH=node_modules npx playwright test --config ../playwright.config.ts
