.PHONY: fixtures clean-fixtures test build publish release

# ── Development ──────────────────────────────────────────────────────────────

test:
	.venv/bin/python -m pytest -q

# ── Release ──────────────────────────────────────────────────────────────────
# Usage:
#   make release VERSION=0.2.0
#     Creates the git commit + tag, builds the wheel, uploads to PyPI.
#
# Requires:
#   pip install build twine
#   A ~/.pypirc or TWINE_TOKEN env var with your PyPI token.

build:
	rm -rf dist/
	.venv/bin/python -m build

publish: build
	.venv/bin/python -m twine upload dist/*

release:
	@test -n "$(VERSION)" || (echo "Usage: make release VERSION=x.y.z" && exit 1)
	git diff --quiet || (echo "Uncommitted changes — commit first" && exit 1)
	git tag v$(VERSION)
	$(MAKE) publish
	git push origin v$(VERSION)
	@echo "Released v$(VERSION)"


# Build two test git repos from the fixtures/ templates.
# Run: make fixtures
# Then try: commitclinic ui --repo fixtures/repos/clean
#           commitclinic ui --repo fixtures/repos/messy
fixtures:
	@echo "==> clean repo"
	rm -rf fixtures/repos/clean
	mkdir -p fixtures/repos
	cp -r fixtures/clean fixtures/repos/clean
	git -C fixtures/repos/clean init -b main
	git -C fixtures/repos/clean config user.email "test@commitclinic.local"
	git -C fixtures/repos/clean config user.name "CommitClinic"
	git -C fixtures/repos/clean add .
	git -C fixtures/repos/clean commit -m "init: clean project"

	@echo "==> messy repo"
	rm -rf fixtures/repos/messy
	cp -r fixtures/messy fixtures/repos/messy
	git -C fixtures/repos/messy init -b main
	git -C fixtures/repos/messy config user.email "test@commitclinic.local"
	git -C fixtures/repos/messy config user.name "CommitClinic"
	git -C fixtures/repos/messy add .
	git -C fixtures/repos/messy commit -m "init: messy project"
	# Add a 6MB binary — will be in git history even after removal
	dd if=/dev/urandom bs=1m count=6 2>/dev/null > fixtures/repos/messy/bigfile.bin
	git -C fixtures/repos/messy add bigfile.bin
	git -C fixtures/repos/messy commit -m "oops: huge binary"
	git -C fixtures/repos/messy rm bigfile.bin
	git -C fixtures/repos/messy commit -m "cleanup: remove huge binary (still in history)"
	# Add binaries to HEAD so scanner flags them
	printf '\x4d\x5a' > fixtures/repos/messy/vendor/tool.exe
	printf '\x4d\x5a' > fixtures/repos/messy/vendor/helper.dll
	git -C fixtures/repos/messy add .
	git -C fixtures/repos/messy commit -m "vendor: windows binaries"

	@echo ""
	@echo "Done. Try:"
	@echo "  commitclinic intake --repo fixtures/repos/clean"
	@echo "  commitclinic ui    --repo fixtures/repos/messy"

clean-fixtures:
	rm -rf fixtures/repos
