# Makefile for mkdocs-beacon-search.
#
# Common targets for local development. Run `make help` for the list.
# CI uses the same scripts (scripts/fetch-widget.sh, scripts/release.py)
# but invokes them directly rather than via this Makefile.

PYTHON          ?= .venv/bin/python
PIP             ?= .venv/bin/pip
RUFF            ?= .venv/bin/ruff
MYPY            ?= .venv/bin/mypy
PYTEST          ?= .venv/bin/pytest
WIDGET_NPM_VERSION ?= 0.1.1

.DEFAULT_GOAL := help
.PHONY: help setup widget lint typecheck test check build clean release update-widget-fixture

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

setup: ## Create .venv and install the project with dev extras.
	python3 -m venv .venv
	$(PIP) install --upgrade pip
	$(PIP) install -e ".[dev]"
	@echo ""
	@echo "Setup complete. Tests rely on a widget bundle on disk; the test"
	@echo "fixture at tests/fixtures/widget/ is auto-copied into place by"
	@echo "tests/conftest.py. Run 'make test' to verify."

widget: ## Pull the pinned widget version from npm into src/beacon_search/static/.
	WIDGET_NPM_VERSION=$(WIDGET_NPM_VERSION) bash scripts/fetch-widget.sh

lint: ## Run ruff.
	$(RUFF) check .

typecheck: ## Run mypy in strict mode.
	$(MYPY)

test: ## Run the test suite.
	$(PYTEST) -v

check: lint typecheck test ## Run lint + typecheck + tests.

build: ## Build sdist and wheel into dist/.
	$(PYTHON) -m build

clean: ## Remove build artifacts and tooling caches.
	rm -rf dist/ build/ src/*.egg-info
	rm -rf .pytest_cache .mypy_cache .ruff_cache
	find . -type d -name __pycache__ -prune -exec rm -rf {} +

release: ## Bump version + CHANGELOG for a release. Usage: make release VERSION=0.2.0
	@if [ -z "$(VERSION)" ]; then \
	    echo "Usage: make release VERSION=x.y.z"; \
	    echo "Example: make release VERSION=0.2.0"; \
	    exit 1; \
	fi
	$(PYTHON) scripts/release.py "$(VERSION)"
	@echo ""
	@echo "Review with 'git diff'. To finalise the release:"
	@echo "  git commit -am 'release: v$(VERSION)'"
	@echo "  git tag v$(VERSION)"
	@echo "  git push origin main v$(VERSION)"

update-widget-fixture: widget ## Refresh tests/fixtures/widget/ from the pinned npm version.
	cp src/beacon_search/static/assets/beacon/beacon-search.umd.js \
	   tests/fixtures/widget/beacon-search.umd.js
	@echo ""
	@echo "Fixture updated to widget v$(WIDGET_NPM_VERSION). Commit the change"
	@echo "alongside any WIDGET_NPM_VERSION bump in release.yml + ci.yml."
