.PHONY: help hooks sync dev backend frontend docs build wheel check lockcheck test migration migrate

# Lockfiles are resolved by CI, never locally: a corporate package mirror
# rewrites artifact URLs and weakens their integrity metadata. UV_FROZEN keeps
# every `uv` call below from silently re-resolving. Override deliberately
# (`make sync UV_FROZEN=0`) only when you intend to change the lockfile.
export UV_FROZEN ?= 1

help:  ## Show available targets
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-10s\033[0m %s\n", $$1, $$2}'

hooks:  ## Install the git hooks (blocks proxy-polluted lockfiles)
	git config core.hooksPath .githooks
	@echo "Hooks enabled from .githooks/"

# uv is the single source for the Python env, running, and building.
# The `dev` dependency group is included by uv automatically, so `uv sync` /
# `uv run` always carry the tooling (ruff/pytest/mypy) — no `--extra dev`.
# `npm ci` (not `install`) installs *from* the lockfile without rewriting it.
sync: ## Install/refresh the dev environment (uv + npm)
	uv sync
	npm --prefix frontend ci
	npm --prefix website ci

# Full dev stack: uvicorn --reload + Vite HMR (Ctrl-C stops both). The Copilot
# SDK is a normal dependency now, so Agents mode needs no extra here — only the
# native CLI it drives, which Settings → Agents provisions on demand.
dev:  ## Run the full dev stack (API + Vite HMR)
	uv run precursor --dev

# Backend only (uvicorn --reload, no Vite).
backend:  ## Run the backend only (uvicorn --reload)
	uv run precursor --dev --no-frontend

# Vite dev server only.
frontend:  ## Run the Vite dev server only
	npm --prefix frontend run dev

# Build the SPA so a plain `uv run precursor` can serve it on one port.
build:  ## Build the SPA into frontend/dist
	npm --prefix frontend run build

# Build the docs with base /docs/ so the app serves them in-app at /docs/.
# (GitHub Pages builds the same source with the default base "/" separately.)
docs:  ## Build the VitePress docs for in-app serving (base /docs/)
	DOCS_BASE=/docs/ npm --prefix website run docs:build

# Build the self-contained wheel + sdist (SPA + docs bundled inside the package).
wheel: build docs  ## Build the distributable wheel + sdist (uv)
	uv build

# Quality gates — mirrors CI (.github/workflows/ci.yml).
check: lockcheck  ## Run all backend + frontend quality gates
	uv run ruff check .
	uv run ruff format --check .
	uv run mypy precursor
	uv run pytest -q
	npm --prefix frontend run typecheck
	npm --prefix frontend run build

lockcheck:  ## Verify lockfiles pin public artifacts with strong hashes
	python3 scripts/check_lockfiles.py

test:  ## Run the backend test suite (uv)
	uv run pytest -q

# Autogenerate a migration from model changes (brings the local DB to head
# first so the diff is correct). Usage: make migration m="add foo to chats".
migration:  ## Autogenerate a migration from model changes (m="description")
	@test -n "$(m)" || { echo 'usage: make migration m="description"'; exit 1; }
	uv run alembic upgrade head
	uv run alembic revision --autogenerate -m "$(m)"

# Apply pending migrations to the configured database.
migrate:  ## Apply pending migrations (alembic upgrade head)
	uv run alembic upgrade head
