# Lucid Train — local build & publish.
#
# Recommended release path is CI: `make tag` pushes a v<version> tag and
# .github/workflows/release.yml builds wheels for every platform. The local
# targets below publish from your machine (current platform only, for PyPI).
#
# Tokens (export before publishing, or use cargo login / ~/.pypirc):
#   MATURIN_PYPI_TOKEN=pypi-...      # PyPI
#   CARGO_REGISTRY_TOKEN=...         # crates.io

VERSION := $(shell grep -m1 '^version' Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/')
DIST    := dist

.DEFAULT_GOAL := help

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

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

# ── checks ────────────────────────────────────────────────────────────────
.PHONY: test
test: ## Run the test suite
	cargo test

.PHONY: fmt
fmt: ## Format the code
	cargo fmt

.PHONY: lint
lint: ## Clippy with warnings denied
	cargo clippy --all-targets -- -D warnings

.PHONY: build
build: ## Release build for the current platform
	cargo build --release

.PHONY: check
check: fmt lint test ## fmt + lint + test (run before releasing)

# ── wheels (maturin) ────────────────────────────────────────────────────────
.PHONY: maturin
maturin: ## Ensure maturin is installed
	@command -v maturin >/dev/null 2>&1 || pipx install maturin

.PHONY: wheel
wheel: maturin ## Build a PyPI wheel for the current platform -> dist/
	maturin build --release --out $(DIST)
	@echo "Built wheels in $(DIST)/ :" && ls -1 $(DIST)/*.whl

# ── publish (local, manual) ─────────────────────────────────────────────────
.PHONY: publish-pypi
publish-pypi: maturin ## Publish to PyPI (current platform wheel only; needs MATURIN_PYPI_TOKEN)
	@test -n "$$MATURIN_PYPI_TOKEN" || { echo "Set MATURIN_PYPI_TOKEN (pypi-...) first"; exit 1; }
	maturin publish --skip-existing

.PHONY: publish-crates
publish-crates: ## Publish to crates.io (needs CARGO_REGISTRY_TOKEN or `cargo login`)
	cargo publish

.PHONY: publish
publish: check publish-pypi publish-crates ## Local publish to PyPI + crates.io
	@echo "Published lucid-train v$(VERSION). NOTE: local PyPI wheel is current-platform only;"
	@echo "use 'make tag' to ship wheels for every platform via CI."

# ── CI release (recommended: all platforms) ─────────────────────────────────
.PHONY: tag
tag: ## Tag v$(VERSION) and push it -> triggers the multi-platform release workflow
	@git diff --quiet || { echo "Working tree is dirty; commit first."; exit 1; }
	git tag -a v$(VERSION) -m "lucid-train v$(VERSION)"
	git push origin v$(VERSION)
	@echo "Pushed tag v$(VERSION). Watch: GitHub -> Actions -> release."

.PHONY: retag
retag: ## Move an existing v$(VERSION) tag to HEAD and re-run CI (unpublished versions only)
	@git diff --quiet || { echo "Working tree is dirty; commit first."; exit 1; }
	-git tag -d v$(VERSION)
	-git push origin :refs/tags/v$(VERSION)
	git tag -a v$(VERSION) -m "lucid-train v$(VERSION)"
	git push origin v$(VERSION)

.PHONY: clean
clean: ## Remove build artifacts
	rm -rf $(DIST)
	cargo clean
