VERSION := $(shell python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")
TAG := v$(VERSION)

.PHONY: help version check-clean check-tag build publish-pypi publish-github release

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

version: ## Print the current version from pyproject.toml
	@echo $(VERSION)

check-clean:
	@if [ -n "$$(git status --porcelain)" ]; then \
		echo "error: working tree is dirty, commit or stash first"; \
		exit 1; \
	fi

check-tag:
	@if git rev-parse "$(TAG)" >/dev/null 2>&1; then \
		echo "error: tag $(TAG) already exists"; \
		exit 1; \
	fi

build: ## Build sdist and wheel into dist/
	rm -rf dist
	uv build

# publish-pypi: build ## Build and publish to PyPI
# 	uv publish

publish-github: check-clean check-tag ## Tag and create a GitHub release
	git tag -a "$(TAG)" -m "Release $(TAG)"
	git push origin "$(TAG)"
	gh release create "$(TAG)" --generate-notes

release: publish-github ## Full release: GitHub tag + release, then PyPI publish
	@echo "$(TAG) released to GitHub and PyPI"
