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

.PHONY: help release tag bump-patch bump-minor build

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

build: ## Build the package locally
	python3 -m build

tag: ## Create a git tag from pyproject.toml version
	git tag -a "v$(VERSION)" -m "v$(VERSION)"

release: tag ## Tag and push (triggers PyPI publish via GitHub Actions)
	git push origin main --tags

bump-patch: ## Bump patch (0.1.0 → 0.1.1), commit, tag, push
	@python3 -c "\
	import tomllib, pathlib;\
	p = pathlib.Path('pyproject.toml');\
	txt = p.read_text();\
	v = tomllib.loads(txt)['project']['version'];\
	parts = v.split('.');\
	parts[-1] = str(int(parts[-1]) + 1);\
	nv = '.'.join(parts);\
	p.write_text(txt.replace(f'version = \"{v}\"', f'version = \"{nv}\"'));\
	print(f'{v} → {nv}')"
	git add pyproject.toml
	git commit -m "release: v$$(python3 -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")"
	$(MAKE) release

bump-minor: ## Bump minor (0.1.0 → 0.2.0), commit, tag, push
	@python3 -c "\
	import tomllib, pathlib;\
	p = pathlib.Path('pyproject.toml');\
	txt = p.read_text();\
	v = tomllib.loads(txt)['project']['version'];\
	parts = v.split('.');\
	parts[1] = str(int(parts[1]) + 1);\
	parts[2] = '0';\
	nv = '.'.join(parts);\
	p.write_text(txt.replace(f'version = \"{v}\"', f'version = \"{nv}\"'));\
	print(f'{v} → {nv}')"
	git add pyproject.toml
	git commit -m "release: v$$(python3 -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")"
	$(MAKE) release
