# Developer tasks. Every target is a command you could run by hand — this
# exists so nobody has to remember which flags CI uses, and so "works on my
# machine" and "passes CI" mean the same thing.
#
# Windows: `make` is not standard there. Every recipe is one line and is
# printed by `make help`, so it can be copied and run directly in PowerShell.
# A task runner that needed installing to read would defeat the purpose.

.DEFAULT_GOAL := help
.PHONY: help install test lint typecheck check build release-check bench clean

PYTHON ?= python3
VENV   ?= .venv
BIN     = $(VENV)/bin

help: ## Show this help
	@grep -hE '^[a-z-]+:.*?## ' $(MAKEFILE_LIST) \
		| awk 'BEGIN{FS=":.*?## "}{printf "  \033[1m%-14s\033[0m %s\n", $$1, $$2}'

install: ## Create a virtualenv and install with dev dependencies
	$(PYTHON) -m venv $(VENV)
	$(BIN)/python -m pip install --upgrade pip
	$(BIN)/python -m pip install -e ".[dev]"

test: ## Run the offline test suite with coverage
	$(BIN)/python -m pytest --cov=heepx --cov-report=term-missing

test-network: ## Run the tests that hit the real registry
	$(BIN)/python -m pytest -m network -v

lint: ## Check formatting and lint rules
	$(BIN)/ruff check .
	$(BIN)/ruff format --check .

format: ## Apply formatting and safe lint fixes
	$(BIN)/ruff check --fix .
	$(BIN)/ruff format .

typecheck: ## Type-check the package
	$(BIN)/mypy src/heepx

check: lint typecheck test ## Everything CI runs on a pull request

build: ## Build the wheel and sdist
	rm -rf dist
	$(BIN)/python -m build

bench: ## Benchmark the operations with published targets
	$(BIN)/python scripts/benchmark.py --network

# The gate before tagging. Deliberately installs the built wheel into a
# throwaway virtualenv rather than testing the checkout: an editable install
# shares nothing with what a user gets, and cannot catch a missing package
# file or an unregistered console script.
release-check: check build ## Full pre-release gate, ending in a clean install
	$(BIN)/python -m twine check dist/* --strict
	rm -rf .release-check
	$(PYTHON) -m venv .release-check
	.release-check/bin/python -m pip install --quiet dist/*.whl
	cd $$(mktemp -d) && \
		$(CURDIR)/.release-check/bin/hx version && \
		$(CURDIR)/.release-check/bin/hx doctor && \
		$(CURDIR)/.release-check/bin/hx init demo && \
		$(CURDIR)/.release-check/bin/hx validate demo
	rm -rf .release-check
	@echo "release-check passed"

clean: ## Remove build, cache and coverage artefacts
	rm -rf dist build .release-check .pytest_cache .ruff_cache .mypy_cache .coverage htmlcov
	find . -name __pycache__ -type d -prune -exec rm -rf {} +
