# Task runner for Linux / macOS (and Windows with GNU make). On Windows without
# make, use .\make.ps1 <target>, which mirrors these targets.
.DEFAULT_GOAL := help
.PHONY: help setup deps node hooks models lock lint format typecheck test coverage check audit-deps sbom fuzz mutate bench eval-search run check-config list-tools inspect inspect-example docker clean

# uv creates and manages .venv. Any uv on PATH will do. With none, get a private
# copy without a global install:
#   python3 -m pip install --target .cache/bootstrap uv
# and run make with UV=.cache/bootstrap/bin/uv
UV     ?= uv
VENV   := .venv
ifeq ($(OS),Windows_NT)
  BIN := $(VENV)/Scripts
else
  BIN := $(VENV)/bin
endif
PY := $(BIN)/python

# Keep every download cache inside the project.
export PIP_CACHE_DIR        := $(CURDIR)/.cache/pip
export UV_CACHE_DIR         := $(CURDIR)/.cache/uv
export npm_config_cache     := $(CURDIR)/.cache/npm
export FASTEMBED_CACHE_PATH := $(CURDIR)/.cache/models
export PRE_COMMIT_HOME      := $(CURDIR)/.cache/pre-commit

help: ## Show targets
	@grep -E '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | awk -F':.*## ' '{printf "  %-10s %s\n", $$1, $$2}'

# Never let uv fetch its own Python into a global folder: use an installed one.
export UV_PYTHON_DOWNLOADS  := never

setup: deps node hooks models ## Create .venv, install pinned deps, Inspector, git hooks, model

deps: ## Create .venv (if needed) and install the exact versions in uv.lock
	$(UV) sync --locked --extra dev

node: ## Install MCP Inspector into ./node_modules (exact lockfile)
	npm ci

hooks: ## Install git pre-commit hooks
	$(BIN)/pre-commit install

MODELS ?= BAAI/bge-base-en-v1.5 BAAI/bge-small-en-v1.5
models: ## Download the embedding models into ./.cache/models (once)
	$(PY) scripts/download_models.py $(MODELS)

# The pinned uv inside .venv does the locking, so the lockfile format does not
# depend on whichever uv happens to be on PATH. requirements.lock is an export of
# uv.lock for tools that read the requirements format (pip-audit, Docker).
lock: ## Re-resolve uv.lock from pyproject.toml and re-export requirements.lock
	$(BIN)/uv lock
	$(BIN)/uv export --locked --extra dev --no-emit-project --format requirements-txt \
		-o requirements.lock -q

lint: ## ruff lint + format check
	$(BIN)/ruff check .
	$(BIN)/ruff format --check .

format: ## Apply ruff fixes and formatting
	$(BIN)/ruff check --fix .
	$(BIN)/ruff format .

typecheck: ## mypy --strict
	$(BIN)/mypy

test: ## pytest
	$(BIN)/pytest

coverage: ## pytest with coverage; policy/ and server.py must stay >= 85%
	$(BIN)/pytest --cov --cov-report=term
	$(BIN)/coverage report --include="*/mcp_gateway/policy/*,*/mcp_gateway/server.py" --fail-under=85

check: lint typecheck test ## All quality gates

audit-deps: ## Scan the locked dependencies for known vulnerabilities
	$(BIN)/pip-audit --strict --require-hashes --disable-pip --cache-dir .cache/pip-audit -r requirements.lock

sbom: ## CycloneDX SBOM of the locked dependencies -> dist/sbom.cdx.json
	mkdir -p dist
	$(BIN)/cyclonedx-py requirements requirements.lock --of JSON -o dist/sbom.cdx.json

FUZZ_EXAMPLES ?= 20000
fuzz: ## Long fuzz run of the SQL/path parsers and the property tests (FUZZ_EXAMPLES per test)
	HYPOTHESIS_PROFILE=fuzz FUZZ_EXAMPLES=$(FUZZ_EXAMPLES) $(BIN)/pytest -q -p no:cacheprovider \
		tests/fuzz tests/test_properties_policy.py tests/test_properties_sanitize.py

MUTATION_MIN ?= 0
mutate: ## Mutation testing of policy/ with mutmut (Linux/macOS); prints the score
	rm -rf mutants
	HYPOTHESIS_PROFILE=mutation $(BIN)/mutmut run
	$(BIN)/mutmut export-cicd-stats
	$(PY) scripts/mutation_score.py --min $(MUTATION_MIN)

bench: ## Cold start, index build, search and per-call overhead -> docs/benchmarks.md
	$(PY) benchmarks/bench.py --out docs/benchmarks.md

eval-search: ## Search accuracy at 50/200/1000 tools, semantic vs keyword vs hybrid -> docs/search-eval.md
	$(PY) evals/search_eval.py --out docs/search-eval.md

run: ## Run the gateway over stdio
	$(PY) -m mcp_gateway

check-config: ## Validate config/gateway.yaml and config/policies.yaml
	$(PY) -m mcp_gateway --check

list-tools: ## Connect to every downstream server and log its tools
	$(PY) -m mcp_gateway --list-tools

inspect: ## Open the gateway in MCP Inspector
	npx --no-install mcp-inspector $(PY) -m mcp_gateway

EXAMPLE ?= weather
inspect-example: ## Open an example server in MCP Inspector (EXAMPLE=weather|sqlite|file)
	npx --no-install mcp-inspector $(PY) examples/$(EXAMPLE)_server.py

docker: ## Build the container image (model baked in)
	docker build -t mcp-gateway:$(shell $(PY) -c "import mcp_gateway; print(mcp_gateway.__version__)") .

clean: ## Remove tool caches (keeps .venv, node_modules, models)
	rm -rf .mypy_cache .ruff_cache .pytest_cache build dist
