VENV_DIR = $(shell pwd)/.venv
PYTHON   = $(VENV_DIR)/bin/python
PIP      = $(VENV_DIR)/bin/pip
PYTEST   = $(VENV_DIR)/bin/pytest
RUFF     = $(VENV_DIR)/bin/ruff

.DEFAULT_GOAL := all
.PHONY: all bootstrap venv install test lint format clean help build dist-check publish-test publish

all: bootstrap test lint

help:
	@echo "erldistpy targets"
	@echo "  make bootstrap   - create venv, install editable + dev deps"
	@echo "  make test        - run pytest"
	@echo "  make lint        - ruff check"
	@echo "  make format      - ruff format"
	@echo "  make clean       - drop venv + caches"
	@echo ""
	@echo "Packaging (PyPI):"
	@echo "  make build       - build sdist + wheel into dist/"
	@echo "  make dist-check  - validate built artifacts (twine check)"
	@echo "  make publish-test- upload to test.pypi.org (needs TWINE_PASSWORD)"
	@echo "  make publish     - upload to pypi.org (needs TWINE_PASSWORD)"
	@echo ""
	@echo "Release flow: bump version in pyproject.toml, commit, then"
	@echo "  git tag -a vX.Y.Z -m 'release vX.Y.Z' && git push --tags"
	@echo "  CI's pypi-twine stage handles the upload from there."

venv:
	test -d $(VENV_DIR) || python3 -m venv $(VENV_DIR)
	$(PIP) install --upgrade pip wheel setuptools

bootstrap: venv
	$(PIP) install -e ".[dev]"

install: bootstrap

test:
	$(PYTEST) -v

lint:
	$(RUFF) check erldistpy tests

format:
	$(RUFF) format erldistpy tests

clean:
	rm -rf $(VENV_DIR)
	rm -rf .pytest_cache .ruff_cache
	rm -rf build dist *.egg-info
	find . -type d -name __pycache__ -exec rm -rf {} +

# -- Packaging --------------------------------------------------------------
# Build sdist + wheel; emits dist/erldistpy-<version>.tar.gz +
# dist/erldistpy-<version>-py3-none-any.whl.
build: bootstrap
	rm -rf dist build
	$(PIP) install --upgrade build
	$(PYTHON) -m build

# Validate built artifacts before upload — twine checks long-description
# rendering and required metadata.
dist-check: build
	$(PIP) install --upgrade twine
	$(VENV_DIR)/bin/twine check dist/*

# Upload to TestPyPI for a dry run before tagging a real release.
# TWINE_USERNAME=__token__ and TWINE_PASSWORD=<test.pypi token> required.
publish-test: dist-check
	$(VENV_DIR)/bin/twine upload --repository testpypi dist/*

# Upload to PyPI — this is what CI runs on tag. Run locally only if you
# explicitly know you want to bypass CI.
publish: dist-check
	$(VENV_DIR)/bin/twine upload dist/*
