# Server targets. ENV_FILE controls which env file the server loads
# (default .env; e.g. `make inspector ENV_FILE=.env.dev`).

ENV_FILE ?= .env

.PHONY: run inspector

run:
	ENV_FILE=$(ENV_FILE) uv run affinity-mcp

inspector:
	npx -y @modelcontextprotocol/inspector \
		-e ENV_FILE=$(ENV_FILE) \
		uv run affinity-mcp

# Eval harness targets (see tests/mcp_eval/README.md).
# AFFINITY_API_KEY must be set in the environment for eval/eval-doctor.

# Scenarios run in parallel via pytest-xdist. Kept low by default: the limiter
# is the shared local stack + Anthropic rate limits, not CPU. Override with
# `make eval MCP_EVAL_WORKERS=8`, or set 0 to run serially for debugging.
MCP_EVAL_WORKERS ?= 4

.PHONY: eval eval-doctor eval-log

eval-doctor:
	tests/mcp_eval/bin/eval-doctor --fast

eval: eval-doctor
	@test -n "$(AFFINITY_API_KEY)" || { echo "AFFINITY_API_KEY is not set — scenarios would all skip."; exit 1; }
	@log="tests/mcp_eval/logs/$$(date -u +%Y-%m-%dT%H-%M-%S).jsonl"; \
		MCP_EVAL_LOG_FILE="$$log" MCP_EVAL_LIVE=1 \
			uv run pytest -m mcp_eval -n $(MCP_EVAL_WORKERS) -v; status=$$?; \
		uv run python tests/mcp_eval/print_log.py "$$log"; \
		exit $$status

eval-log:
	uv run python tests/mcp_eval/print_log.py

# Complexity gate (cyclomatic/cognitive/CRAP/function-tokens/file-lines).
# `check-complexity` is a global tool from affinity-valence (uv tool install
# --editable . in that repo). Run `uv run coverage json` first for CRAP scores.

.PHONY: complexity

complexity:
	check-complexity src

# MCP Apps frontend (src/affinity_mcp/apps).

.PHONY: build-apps test-apps

build-apps:
	pnpm --dir src/affinity_mcp/apps run build

test-apps:
	pnpm --dir src/affinity_mcp/apps run test

# Full local dev setup. Run after cloning, or after pulling changes that
# touch pyproject.toml or src/affinity_mcp/apps, to get everything (Python
# deps + built MCP Apps bundles) in place for `make run`/`make inspector`.

.PHONY: local-build

local-build:
	uv sync --dev
	pnpm --dir src/affinity_mcp/apps install --frozen-lockfile
	$(MAKE) build-apps
