.PHONY: help install test lint format typecheck check clean contracts rpc-contract

VENV := .venv/bin

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

# ``uv pip``, not ``$(VENV)/pip``: a uv-made venv (``uv venv`` / ``uv
# sync``, which is how this repo's own .venv is provisioned) seeds no
# pip at all, so the old recipe died with "make: ... Error 127" —
# the documented one-command setup failing on the documented toolchain.
# ``$(VENV)/python -m pip`` is no fix either; the module isn't there.
# Extras are the four pyproject actually declares. ``knowledge`` is not
# one of them and never has been; an unknown extra only warns, so it
# installed nothing and said so in a line nobody read.
install: ## Install package in dev mode with all extras
	uv pip install --python $(VENV)/python -e ".[dev,mcp,gemini,web,decisions]"

test: ## Run tests
	$(VENV)/python -m pytest tests/ -q

test-v: ## Run tests with verbose output
	$(VENV)/python -m pytest tests/ -v

test-cov: ## Run tests with coverage
	$(VENV)/python -m pytest tests/ --cov=igni_core --cov-report=term-missing

lint: ## Run ruff linter
	$(VENV)/ruff check src/ tests/

format: ## Format code with ruff
	$(VENV)/ruff format src/ tests/

format-check: ## Check formatting without making changes
	$(VENV)/ruff format --check src/ tests/

typecheck: ## Run mypy type checking
	$(VENV)/mypy src/igni_core/

check: lint format-check typecheck test ## Run all checks (lint + format + typecheck + test)

fix: ## Auto-fix linting and formatting issues
	$(VENV)/ruff check --fix src/ tests/
	$(VENV)/ruff format src/ tests/

# What igni-core publishes for the other repositories: the host object
# model igni-sdk generates both SDKs from, the RPC contract igni-sdk
# generates its typed client from, and the wire schema igni-app's web
# client tests its reads against. A test fails while any is stale.
contracts: ## Write the contracts igni-core publishes (docs/host-schema.json, docs/rpc-contract.json, docs/wire-schema.json)
	$(VENV)/python scripts/export_contracts.py

rpc-contract: ## Write docs/rpc-contract.json, the RPC contract igni-sdk generates its client from
	$(VENV)/python -c "from igni_core.protocol import rpc_contract; print('wrote', rpc_contract.write()[0])"

clean: ## Remove build artifacts and caches
	rm -rf build/ dist/ *.egg-info src/*.egg-info
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name .mypy_cache -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name .pytest_cache -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name .ruff_cache -exec rm -rf {} + 2>/dev/null || true
