.PHONY: install
install: ## Install the virtual environment
	@echo "🚀 Creating virtual environment using uv"
	@uv sync

.PHONY: check
check: ## Run code quality tools.
	@echo "🚀 Checking lock file consistency with 'pyproject.toml'"
	@uv lock --locked
	@uv run ruff check --exit-non-zero-on-fix
	@uv run ruff format
	@echo "🚀 Static type checking: Running mypy"
	@uv run mypy
	@echo "🚀 Checking unused call results: Running basedpyright"
	@uv run basedpyright src
	@echo "🚀 Checking for obsolete dependencies: Running deptry"
	@uv run deptry src

.PHONY: test
test: ## Test the code with pytest
	@echo "🚀 Testing code: Running pytest"
	@uv run python -m pytest --cov --cov-config=pyproject.toml --cov-report=xml

.PHONY: test-unit
test-unit: ## Run the unit tests only (fast; no Maude interpreter)
	@echo "🚀 Testing code: Running unit tests"
	@uv run python -m pytest tests/unit

# `-n auto` adapts to the machine (affinity-aware on Python 3.13+), so this works on
# the local dev box and on smaller CI runners. Cap it on shared machines with
# PYTEST_XDIST_AUTO_NUM_WORKERS=<n>. `make test` stays sequential for CI/release.
.PHONY: test-integration
test-integration: ## Run the integration tests only, in parallel (spawns the real Maude interpreter; slower than unit)
	@echo "🚀 Testing code: Running integration tests (pytest-xdist, -n auto)"
	@uv run python -m pytest tests/integration -n auto


.PHONY: run
run: ## Run the server
	@echo "🚀 Running server"
	@uv run harold-mcp

.PHONY: build
build: clean-build ## Build wheel file
	@echo "🚀 Creating wheel file"
	@uvx --from build pyproject-build --installer uv

.PHONY: clean-build
clean-build: ## Clean build artifacts
	@echo "🚀 Removing build artifacts"
	@uv run python -c "import shutil; import os; shutil.rmtree('dist') if os.path.exists('dist') else None"

.PHONY: publish
publish: ## Publish a release to PyPI.
	@echo "🚀 Publishing."
	@uvx twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

.PHONY: build-and-publish
build-and-publish: build publish ## Build and publish.

.PHONY: docs-test
docs-test: ## Test if documentation can be built without warnings or errors
	@uv run mkdocs build -s

.PHONY: docs
docs: ## Build and serve the documentation
	@uv run mkdocs serve

.PHONY: release
release: install check test docs-test  ## run all CI tests
	@echo
	@echo "🚀 All CI tests are passing!"

.PHONY: help
help:
	@uv run python -c "import re; \
	[[print(f'\033[36m{m[0]:<20}\033[0m {m[1]}') for m in re.findall(r'^([a-zA-Z_-]+):.*?## (.*)$$', open(makefile).read(), re.M)] for makefile in ('$(MAKEFILE_LIST)').strip().split()]"

.DEFAULT_GOAL := help
