.PHONY: all lint format lint-fix test help start_mcp test_integration

# Default target executed when no arguments are given to make
all: help

######################
# TESTING AND COVERAGE
######################

# Define a variable for the test file path.
TEST ?= tests/unit_tests

test:
	uv run pytest --disable-socket --allow-unix-socket $(TEST)

unit-tests:
	uv run pytest --disable-socket --allow-unix-socket tests/unit_tests

test_watch:
	uv run ptw . -- $(TEST)

.PHONY: test_integration

INTEGRATION_TEST ?= tests/integration_tests

test_integration:
	@echo "Starting MCP servers and running integration tests..."; \
	cleanup() { \
		echo "Cleaning up servers..."; \
		lsof -ti:8001 | xargs kill -9 2>/dev/null || true; \
		lsof -ti:9001 | xargs kill -9 2>/dev/null || true; \
		lsof -ti:8081 | xargs kill -9 2>/dev/null || true; \
	}; \
	echo "Starting servers in background..."; \
	uv run python tests/server/weather_http_server.py >/dev/null 2>&1 & \
	uv run python tests/server/tools_sse_server.py >/dev/null 2>&1 & \
	uv run python tests/server/websocket_server.py >/dev/null 2>&1 & \
	sleep 2; \
	echo "Running integration tests..."; \
	uv run pytest $(INTEGRATION_TEST); \
	test_result=$$?; \
	cleanup; \
	exit $$test_result

start_mcp:
	# start mock mcp server for integration tests
	uv run python tests/server/mock_mcp_server.py


run_integration_test:
	uv run pytest tests/integration_tests


######################
# LINTING AND FORMATTING
######################

# Define a variable for Python and notebook files.
lint format: PYTHON_FILES=.
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=. --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')

lint lint_diff:
	[ "$(PYTHON_FILES)" = "" ] ||	uv run ruff format $(PYTHON_FILES) --diff
	[ "$(PYTHON_FILES)" = "" ] ||	uv run ruff check $(PYTHON_FILES) --diff
	# [ "$(PYTHON_FILES)" = "" ] || uv run mypy $(PYTHON_FILES)

format format_diff:
	[ "$(PYTHON_FILES)" = "" ] || uv run ruff format $(PYTHON_FILES)
	[ "$(PYTHON_FILES)" = "" ] || uv run ruff check --fix $(PYTHON_FILES)

lint-fix:
	uv run ruff format $(PYTHON_FILES)
	uv run ruff check --fix $(PYTHON_FILES)


generate_examples:
	uv run langgraph-gen examples/agentic_rag/spec.yml --language python
	uv run langgraph-gen examples/agentic_rag/spec.yml --language typescript
	uv run langgraph-gen examples/rag/spec.yml --language python
	uv run langgraph-gen examples/rag/spec.yml --language typescript
	

######################
# HELP
######################

help:
	@echo '===================='
	@echo '-- LINTING --'
	@echo 'format                       - run code formatters'
	@echo 'lint                         - run linters'
	@echo 'spell_check                 	- run codespell on the project'
	@echo 'spell_fix                		- run codespell on the project and fix the errors'
	@echo '-- TESTS --'
	@echo 'coverage                     - run unit tests and generate coverage report'
	@echo 'test                         - run unit tests'
	@echo 'test TEST_FILE=<test_file>   - run all tests in file'
	@echo '-- DOCUMENTATION tasks are from the top-level Makefile --'

