.DEFAULT_GOAL := help
VENV := .venv
PY := $(VENV)/bin/python
PIP := $(VENV)/bin/pip
PROFILE ?=

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

.PHONY: sync-templates
sync-templates: ## Refresh bundled generator templates (run before publish)
	$(PY) tools/sync_generator_templates.py

.PHONY: setup
setup: sync-templates ## Create venv and install dev dependencies
	@test -d $(VENV) || python3 -m venv $(VENV)
	$(PIP) install --upgrade pip
	$(PIP) install -e ".[all,dev]"

.PHONY: run
run: ## Run the reference server (PROFILE=prod to override)
	$(PY) -m agent_server run $(if $(PROFILE),--profile $(PROFILE),)

.PHONY: run-http
run-http: ## Run with HTTP on :8000/mcp and ops on :8080 (no auth)
	$(PY) -m agent_server run --profile local-http

.PHONY: example
example: ## Call the sample ping tool in-process (no server required)
	$(PY) examples/call_ping.py

.PHONY: example-http
example-http: ## Call ping over Streamable HTTP MCP (make run-http in another terminal)
	$(PY) examples/call_ping_http.py

.PHONY: docs
docs: ## Serve the live docs server (generated from the registry)
	$(PY) -m agent_server docs

.PHONY: fmt
fmt: ## Auto-format code
	$(VENV)/bin/ruff format src tests
	$(VENV)/bin/ruff check --fix src tests

.PHONY: lint
lint: ## Lint code
	$(VENV)/bin/ruff format --check src tests
	$(VENV)/bin/ruff check src tests

.PHONY: type
type: ## Strict type check
	$(VENV)/bin/mypy src

.PHONY: deadcode
deadcode: ## Detect dead / unreachable code
	$(VENV)/bin/vulture

.PHONY: factcheck
factcheck: ## Run golden-path fact-checks against this project
	$(PY) tools/checks/run_checks.py .

.PHONY: typingcheck
typingcheck: ## Run architecture/typing invariants (ops models, transport boundaries)
	$(PY) tools/checks/validate_typing.py .

.PHONY: check
check: lint type deadcode factcheck typingcheck ## Run all quality gates

.PHONY: test
test: ## Run unit + integration tests
	$(VENV)/bin/pytest

.PHONY: verify-generated
verify-generated: ## Generate example projects and run their make check/test/example
	$(PY) tools/verify_generated.py --output build/generated-verify

.PHONY: proto
proto: ## Compile gRPC protobufs
	$(PY) -m grpc_tools.protoc -Iproto --python_out=src/agent_server/transports/grpc --grpc_python_out=src/agent_server/transports/grpc proto/agent_server.proto

.PHONY: all
all: check test verify-generated ## Full gate: checks + tests + generated projects
