.DEFAULT_GOAL := help

PYTHON ?= $(shell [ -x .venv/bin/python ] && printf '%s' .venv/bin/python || printf '%s' python3)
PYTEST ?= $(shell [ -x .venv/bin/pytest ] && printf '%s' .venv/bin/pytest || printf '%s' pytest)
MATURIN ?= $(shell [ -x .venv/bin/maturin ] && printf '%s' .venv/bin/maturin || printf '%s' maturin)
RUFF ?= $(shell [ -x .venv/bin/ruff ] && printf '%s' .venv/bin/ruff || printf '%s' ruff)
CARGO ?= cargo

.PHONY: help build develop fmt test itest compliance vet clean

help: ## List all available targets with descriptions
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  %-10s %s\n", $$1, $$2}'

build: ## Build a wheel/sdist with maturin
	$(MATURIN) build

develop: ## Build and install the extension into the active Python environment
	$(MATURIN) develop

fmt: ## Format Python sources with Ruff
	$(RUFF) format .

test: develop ## Run default tests (excluding integration and compliance suites)
	$(PYTEST) -m "not integration and not compliance"

itest: develop ## Run Python integration tests against real loopback nodes
	$(PYTEST) tests/ -m integration -v

compliance: develop ## Run cross-language compliance tests against the Rust CLI
	$(PYTEST) tests/test_compliance.py -m compliance -v

vet: ## Run all Python and Rust format and lint checks
	$(RUFF) format --check .
	$(RUFF) check .
	$(CARGO) fmt --check
	$(CARGO) clippy --all-targets -- -D warnings

clean: ## Remove generated build artefacts and local test caches
	$(CARGO) clean
	rm -rf .pytest_cache build dist htmlcov .maturin
	rm -rf python/igc_net/__pycache__ tests/__pycache__
	rm -f python/igc_net/*.so python/igc_net/*.pyd python/igc_net/*.dll python/igc_net/*.dylib
	rm -rf python/igc_net/*.dSYM
