.DEFAULT_GOAL := help

# Python versions for `make test-matrix`. Override with: make test-matrix PYTHONS="3.11 3.12"
PYTHONS ?= 3.11 3.12 3.13 3.14 3.15

# Use the activated virtualenv if there is one, otherwise uv's project .venv.
UV_ENV := $(if $(VIRTUAL_ENV),--active)
RUN := uv run $(UV_ENV)

.PHONY: help install lint format test cov test-matrix pre-commit check build publish upgrade clean

help: ## Show available commands
	@awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z_-]+:.*## / {printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

install: ## Install the package and dev tools, and set up git hooks
	uv sync $(UV_ENV)
	$(RUN) pre-commit install

lint: ## Lint with ruff and check formatting
	$(RUN) ruff check .
	$(RUN) ruff format --check .

format: ## Auto-fix lint issues and format code
	$(RUN) ruff check --fix .
	$(RUN) ruff format .

test: ## Run tests on the current Python
	$(RUN) pytest

cov: ## Run tests with a coverage report
	$(RUN) pytest --cov=agentbuild --cov-report=term-missing

test-matrix: ## Run tests on every Python version in PYTHONS
	@for v in $(PYTHONS); do \
		echo "==> Python $$v"; \
		uv run --isolated --python $$v pytest -q || exit 1; \
	done

pre-commit: ## Run all pre-commit hooks on all files
	$(RUN) pre-commit run --all-files

check: lint test ## Run the same checks as CI

build: ## Build the sdist and wheel into dist/
	rm -rf dist
	uv build

publish: build ## Build and upload to PyPI
	uv publish

upgrade: ## Upgrade locked dependencies and pre-commit hook versions
	uv lock --upgrade
	$(RUN) pre-commit autoupdate

clean: ## Remove build artifacts and caches
	rm -rf build dist .pytest_cache .ruff_cache .coverage htmlcov coverage.xml
	find src tests -type d -name __pycache__ -exec rm -rf {} +
