.DELETE_ON_ERROR:
MAKEFLAGS += --no-builtin-rules

VENV := .venv
PYTHON := $(VENV)/bin/python3
UV := uv

.PHONY: all
all:
	$(MAKE) fmt
	$(MAKE) lint
	$(MAKE) test

.PHONY: venv
venv:
	test -d $(VENV) || $(UV) venv $(VENV)
	$(UV) pip install --prerelease=allow -e '.[dev]' --python $(PYTHON)

.PHONY: fmt
fmt: venv
	$(VENV)/bin/ruff format .

.PHONY: lint
lint: venv
	$(VENV)/bin/ruff format --check .
	$(VENV)/bin/ruff check .

.PHONY: test
test: venv
	$(VENV)/bin/pytest

.PHONY: clean
clean:
	find . -name '__pycache__' -type d -prune -exec rm -rf {} +
	rm -rf .pytest_cache .ruff_cache *.egg-info

.PHONY: distclean
distclean: clean
	rm -rf $(VENV)

.PHONY: help
help:
	@echo "Targets:"
	@echo "  all        fmt + lint + test (default)"
	@echo "  venv       create .venv and install the package with dev deps"
	@echo "  fmt        format code with ruff"
	@echo "  lint       check formatting and lint with ruff"
	@echo "  test       run pytest"
	@echo "  clean      remove caches and build artifacts"
	@echo "  distclean  clean + remove .venv"
