# Common tasks, so "how do I run the tests" has one answer that does not depend
# on which shell or virtualenv someone happens to be in.
#
# `make check` is what CI runs. If it passes locally it passes there — that
# parity is the point, and scripts/ci-local.sh enforces the Python version too.

.DEFAULT_GOAL := help
PYTHON ?= python3

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

.PHONY: install
install:  ## Install the package and every development dependency
	$(PYTHON) -m pip install -e ".[dev]"
	pre-commit install

.PHONY: check
check:  ## Everything CI runs, against the version CI runs it on
	./scripts/ci-local.sh

.PHONY: test
test:  ## Run the test suite
	pytest

.PHONY: test-cov
test-cov:  ## Run the tests with a coverage report
	pytest --cov --cov-report=term-missing --cov-report=html

.PHONY: lint
lint:  ## Lint and check formatting without changing anything
	ruff check .
	black --check .

.PHONY: format
format:  ## Apply formatting and safe lint fixes
	black .
	ruff check --fix .

.PHONY: types
types:  ## Type-check in strict mode
	mypy src tests

.PHONY: security
security:  ## Static security scan
	bandit -r src -c pyproject.toml

.PHONY: docs-check
docs-check:  ## Verify links, documented formats and packaging claims
	pytest tests/contract -q

.PHONY: golden
golden:  ## Regenerate the golden files — read the diff before committing it
	UPDATE_GOLDEN=1 pytest tests/contract/test_golden.py

.PHONY: build
build:  ## Build the wheel and sdist, and validate them
	rm -rf dist
	$(PYTHON) -m build
	$(PYTHON) -m twine check dist/*

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