.PHONY: help install install-cli install-skill uninstall reinstall sync test \
	lint format typecheck check hooks build publish clean tag release

VERSION := $(shell grep -m1 '^version' pyproject.toml | cut -d'"' -f2)

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

install: install-cli install-skill  ## Install the luge CLI globally + the Claude Code skill

install-cli:  ## Install/upgrade the `luge` command globally (editable)
	uv tool install --editable . --force

install-skill:  ## Install the luge-platform skill into ~/.claude/skills
	luge-cli claude skill install --force

uninstall:  ## Remove the CLI and the skill
	-uv tool uninstall luge-cli
	-rm -rf $(HOME)/.claude/skills/luge-platform

reinstall: uninstall install  ## Clean reinstall

sync:  ## Create/refresh the local dev environment
	uv sync

test:  ## Run the test suite
	uv run pytest -q

lint:  ## Lint with ruff (check only)
	uv run ruff check .

format:  ## Auto-fix lint issues and format with ruff
	uv run ruff check --fix .
	uv run ruff format .

typecheck:  ## Type-check with mypy
	uv run mypy src/luge_cli

check: lint typecheck test  ## Lint + type-check + test (what CI runs)
	uv run ruff format --check .

hooks:  ## Install the pre-commit git hooks
	uv run pre-commit install

clean:  ## Remove build artifacts
	rm -rf dist build ./*.egg-info

build: clean check  ## Build the sdist + wheel (runs checks first)
	uv build

# Uploads to the repository configured in ~/.pypirc (override: make publish REPO=testpypi).
REPO ?= pypi
publish: build  ## Build and upload to PyPI (via twine + ~/.pypirc)
	uv run twine check dist/*
	uv run twine upload -r $(REPO) dist/*

tag:  ## Tag the current version (vX.Y.Z) and push the tag
	git tag -a v$(VERSION) -m "v$(VERSION)"
	git push origin v$(VERSION)

release: tag  ## Tag + create a GitHub release with notes from CHANGELOG.md
	@awk '/^## \[$(VERSION)\]/{f=1;next} /^## \[/{f=0} f' CHANGELOG.md > .release-notes.md
	gh release create v$(VERSION) --title "v$(VERSION)" --notes-file .release-notes.md
	@rm -f .release-notes.md
