.PHONY: install check lint format test audit clean docs-serve docs-build type-check help

# 🚀 Workspace Management

install:  ## Install all dependencies
	uv sync --all-groups

# 🛡️ Quality Gates

lint:  ## Linter + format check
	uv run ruff check .
	uv run ruff format --check .

format:  ## Auto-format code
	uv run ruff format .

# Les packages sont DÉCOUVERTS depuis [tool.uv.workspace] members = ["packages/*"] :
# ajouter un package ne demande aucune édition ici. Une liste en dur finit toujours
# par diverger du workspace — c'est ainsi que des packages échappent au type-check
# sans bruit.
# Le répertoire de tests est DÉRIVÉ du nom du package (tests_<module>) : un renommage
# ou un nouveau package ne laisse pas de chemin littéral orphelin.
MYPY_PACKAGES := $(notdir $(patsubst %/,%,$(dir $(wildcard packages/*/pyproject.toml))))

# Un mypy global (`mypy packages/`) ignorerait la config propre à chaque package :
# chaque membre est vérifié avec son propre pyproject.toml, dans son environnement.
type-check:  ## Run mypy per package
	@echo "🔍 Running mypy per package..."
	@for pkg in $(MYPY_PACKAGES); do \
		tests="packages/$$pkg/tests_$$(echo $$pkg | tr - _)"; \
		[ -d "$$tests" ] || { echo "❌ $$pkg: $$tests introuvable"; exit 1; }; \
		uv run --package $$pkg mypy --config-file packages/$$pkg/pyproject.toml \
			packages/$$pkg/src "$$tests" || exit 1; \
	done

audit:  ## Security audit
	uv run pip-audit

# 🧪 Testing

test:  ## Run all workspace tests
	uv run pytest packages/ -q

# 📚 Documentation

docs-serve:  ## Serve documentation locally
	uv run mkdocs serve

docs-build:  ## Build documentation
	uv run mkdocs build

# ✅ Aggregate

check:  ## Full quality gate (lint + type-check + test)
	$(MAKE) lint
	$(MAKE) type-check
	$(MAKE) test

# 🧹 Housekeeping

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

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