.PHONY: help dev run build test test-docker lint-test ci-lint-test check fmt clippy lint clean supergraph install
.PHONY: build_deploy up_deploy up_internal up_mcp
.PHONY: test_integration ci_test_integration ci_test_integration_clean
.PHONY: test_integration_local_public test_integration_local_internal test_integration_local_mcp

# Default target
help:
	@echo "Available targets:"
	@echo "  make dev          - Run router in dev mode with config.yaml and hot-reload"
	@echo "  make run          - Same as dev"
	@echo "  make build        - Build the project"
	@echo "  make test         - Run all tests"
	@echo "  make test-docker  - Run tests in Docker (slower but consistent with CI)"
	@echo "  make lint-test    - Run clippy and tests (native cargo)"
	@echo "  make ci-lint-test - Run linting and tests in Docker (used by CI)"
	@echo "  make check        - Check for compilation errors without building"
	@echo "  make fmt          - Format code with cargo fmt"
	@echo "  make clippy       - Run clippy linter"
	@echo "  make lint         - Alias for clippy"
	@echo "  make supergraph   - Generate supergraph schema"
	@echo "  make install      - Install the router binary"
	@echo "  make clean        - Clean build artifacts"
	@echo "  make test_integration - Run JWT enforcement tests against the QA routers (public/internal/mcp)"
	@echo "  make test_integration_local_<public|internal|mcp> - Same in Docker, against the matching local router (make up_deploy/up_internal/up_mcp)"
	@echo "  make ci_test_integration - Run the JWT enforcement tests in Docker (used by CI; needs AWS creds)"

# Run in dev mode with config.yaml and hot-reload
dev:
	cargo run -- --dev --config config.yaml --hot-reload

# Alias for dev
run: dev

# Build the project
build:
	cargo build

# Run tests
test:
	cargo test

# Run tests in Docker (slower but consistent with CI)
test-docker:
	docker compose up --build --exit-code-from lint-test --abort-on-container-exit --remove-orphans lint-test

# Run clippy and tests
lint-test:
	cargo clippy
	cargo test

# Run linting and tests in Docker (used by CI)
ci-lint-test:
	docker compose up --build --exit-code-from lint-test --abort-on-container-exit --remove-orphans lint-test

# Check for compilation errors
check:
	cargo check

# Format code
fmt:
	cargo fmt

# Run clippy
clippy:
	cargo clippy

# Alias for clippy (lint)
lint: clippy

# JWT auth enforcement tests (Python, tests/). Hit the deployed QA routers:
# public must pass invalid auth (Warning mode), internal/mcp must 401 (Block).
test_integration:
	cd tests && uv run pytest

# JWT enforcement tests in Docker against a local router. Start the matching
# router first (make up_deploy / up_internal / up_mcp). internal/mcp get
# LOCAL_ROUTER_URL/MODE from their compose override files; the base compose
# file is already the public variant but is shared with CI, so the public
# target passes the URL itself. --build so edits under tests/ are picked up
# (they are baked into the image).
test_integration_local_public:
	LOCAL_ROUTER_URL=http://router-public:8080/graphql docker compose up --build integration-test

test_integration_local_internal:
	docker compose -f docker-compose.yaml -f docker-compose-internal.yaml up --build integration-test

test_integration_local_mcp:
	docker compose -f docker-compose.yaml -f docker-compose-mcp.yaml up --build integration-test

# JWT enforcement tests in Docker (CI). Requires AWS credentials in the
# environment to mint a valid QA token; forged-token cases run without them.
ci_test_integration:
	docker compose up \
		--build \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--remove-orphans \
		integration-test

ci_test_integration_clean:
	docker compose down integration-test --remove-orphans

# Generate supergraph schema
supergraph:
	./scripts/generate-supergraph.sh

# Install the router binary
install:
	cargo install --locked --path .

# Clean build artifacts
clean:
	cargo clean

build_deploy:
	docker compose build router

up_deploy: .env
	source .env && docker compose up router

up_internal: .env
	source .env && docker compose -f docker-compose.yaml -f docker-compose-internal.yaml up router

up_mcp: .env
	source .env && docker compose -f docker-compose.yaml -f docker-compose-mcp.yaml up router
