# Morpheus Development Makefile

.PHONY: help install install-libs install-tools test test-libs test-tools clean lint format check docs

# Default target
help:
	@echo "Morpheus Development Commands"
	@echo ""
	@echo "Installation (Single .venv - Simple & Stable):"
	@echo "  install       Install everything in shared .venv at repo root"
	@echo "  install-full  Same as install (activate .venv to use)"
	@echo "  install-manual Use manual package list"
	@echo "  discover      Show what would be installed (dry-run)"
	@echo "  list-packages List all discovered Python packages"
	@echo "  reinstall     Force reinstall all packages"
	@echo ""
	@echo "Testing (UV-Based):"
	@echo "  test                Run key tests (learning + learning-loop)"
	@echo "  test-learning       Test libs/learning"
	@echo "  test-learning-loop  Test tools/learning-loop"
	@echo "  test-libs           Test all libraries"
	@echo ""
	@echo "Code Quality:"
	@echo "  lint          Run linting on all packages"
	@echo "  format        Format code with ruff"
	@echo "  check         Run type checking"
	@echo ""
	@echo "Development:"
	@echo "  clean         Clean build artifacts"
	@echo "  status        Show installation status"
	@echo "  docs          List available documentation"

# Installation targets (Single .venv)
install:
	@echo "Installing Morpheus (single shared .venv)..."
	./install.sh

install-full:
	@echo "Installing Morpheus (single shared .venv)..."
	./install.sh

install-legacy:
	@echo "⚠️  Using legacy pip-based install (may have pyenv conflicts)..."
	./install-dev-auto.sh

install-manual:
	@echo "⚠️  Using legacy manual install (may have pyenv conflicts)..."
	./install-dev.sh

install-libs:
	@echo "Installing Morpheus libraries..."
	./install-dev.sh --libs-only

install-tools:
	@echo "Installing Morpheus tools..."
	./install-dev.sh --tools-only

discover:
	@echo "Discovering Python packages..."
	./install-dev-auto.sh --dry-run

reinstall:
	@echo "Force reinstalling all packages..."
	./install-dev.sh --force

# Testing targets (UV-based)
test:
	@echo "Running all tests..."
	@cd libs/learning && uv run pytest -v
	@cd tools/learning-loop && uv run pytest -v

test-libs:
	@echo "Testing libs/learning..."
	@cd libs/learning && uv run pytest -v

test-learning:
	@echo "Testing learning library..."
	@cd libs/learning && uv run pytest -v

test-learning-loop:
	@echo "Testing learning-loop tool..."
	@cd tools/learning-loop && uv run pytest -v

test-tools:
	@echo "Running tests for tools..."
	@$(call test-packages,tools)

test-all-auto:
	@echo "Auto-discovering and testing all packages..."
	@$(call test-packages,libs)
	@$(call test-packages,tools)
	@$(call test-packages,apps)
	@$(call test-packages,agents)

# Function to test packages in a directory
# Exit codes: 0=pass, 1=fail, 2=interrupted/error, 5=no tests collected
define test-packages
	@for pkg in $(1)/*/; do \
		if [ -f "$$pkg/pyproject.toml" ]; then \
			pkg_name=$$(basename "$$pkg"); \
			if [ -d "$$pkg/tests" ]; then \
				echo "Testing $$pkg_name..."; \
				cd "$$pkg" && uv sync --all-extras --quiet && uv run pytest -v; \
				test_exit=$$?; \
				cd - > /dev/null; \
				if [ $$test_exit -eq 1 ]; then \
					echo "❌ Tests failed in $$pkg_name"; \
					exit 1; \
				elif [ $$test_exit -eq 2 ]; then \
					echo "⚠️  Test collection errors in $$pkg_name (exit $$test_exit)"; \
					exit 1; \
				elif [ $$test_exit -eq 5 ]; then \
					echo "  No tests collected in $$pkg_name"; \
				fi; \
			else \
				echo "  No tests found for $$pkg_name"; \
			fi; \
		fi; \
	done
endef

test-config:
	@echo "Testing configuration library..."
	cd libs/configuration && uv run pytest -v

test-models:
	@echo "Testing data models library..."
	cd libs/data-models && uv run pytest -v

test-paths:
	@echo "Testing path resolution library..."
	cd libs/path-resolution && uv run pytest -v || echo "No tests yet for path-resolution"

# Code quality targets
lint:
	@echo "Running linting on all packages..."
	@for dir in libs/*/ tools/*/; do \
		if [ -f "$$dir/pyproject.toml" ]; then \
			echo "Linting $$dir..."; \
			cd "$$dir" && ruff check src/ || true; \
			cd - > /dev/null; \
		fi; \
	done

format:
	@echo "Formatting code with ruff..."
	@for dir in libs/*/ tools/*/; do \
		if [ -f "$$dir/pyproject.toml" ]; then \
			echo "Formatting $$dir..."; \
			cd "$$dir" && ruff format src/ || true; \
			cd - > /dev/null; \
		fi; \
	done

check:
	@echo "Running type checking..."
	@for dir in libs/*/ tools/*/; do \
		if [ -f "$$dir/pyproject.toml" ]; then \
			echo "Type checking $$dir..."; \
			cd "$$dir" && mypy src/ || true; \
			cd - > /dev/null; \
		fi; \
	done

# Development targets
clean:
	@echo "Cleaning build artifacts and UV state..."
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "dist" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "build" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".venv" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "uv.lock" -exec rm -f {} + 2>/dev/null || true
	@echo "✓ Clean complete"

docs:
	@echo "Documentation is in each package's README.md file"
	@echo ""
	@echo "Available documentation:"
	@for dir in libs/*/ tools/*/; do \
		if [ -f "$$dir/README.md" ]; then \
			echo "  $$dir/README.md"; \
		fi; \
	done

status:
	@echo "Morpheus Installation Status"
	@echo "=============================="
	@echo ""
	@echo "Libraries:"
	@python -c "\
import importlib.util; \
libs = ['configuration', 'data_models', 'path_resolution', 'telemetry']; \
[print(f'   {lib}') if importlib.util.find_spec(lib) else print(f'   {lib}') for lib in libs]"
	@echo ""
	@echo "CLI Tools:"
	@which morpheus > /dev/null 2>&1 && echo "   morpheus" || echo "   morpheus"
	@which search > /dev/null 2>&1 && echo "   search" || echo "   search"
	@echo ""
	@echo "Python Environment:"
	@echo "  Python: $$(python --version)"
	@echo "  Virtual Env: $${VIRTUAL_ENV:-None}"
	@echo "  Pip: $$(pip --version)"

# Development workflow targets
dev-setup: install test
	@echo "Development environment setup complete!"

quick-test:
	@echo "Running quick smoke tests..."
	@python -c "\
from morpheus.libs.configuration import Config; \
from morpheus.libs.data_models import ObjectScriptWorkflowConfig; \
from morpheus.libs.path_resolution import morpheus_paths; \
print(' All core libraries import successfully')"

# CI/CD targets
ci-test: install test lint check
	@echo "CI pipeline complete!"

# Package-specific targets for the newly implemented libraries
test-new-libs: test-config test-models test-paths
	@echo "All new libraries tested successfully!"

install-new-libs:
	@echo "Installing newly implemented libraries..."
	pip install -e libs/configuration[dev]
	pip install -e libs/data-models[dev]
	pip install -e libs/path-resolution[dev]
	@echo "New libraries installed!"

# Help for specific commands
help-install:
	@echo "Installation Commands:"
	@echo "  make install       - Install all packages in editable mode"
	@echo "  make install-libs  - Install only libraries"
	@echo "  make install-tools - Install only CLI tools"
	@echo "  make reinstall     - Force reinstall all packages"

list-packages:
	@echo "Discovered Python Packages:"
	@echo "============================="
	@echo ""
	@echo "📚 Libraries (libs/):"
	@find libs -maxdepth 2 -name "pyproject.toml" 2>/dev/null | while read f; do echo "  - $$(basename $$(dirname $$f))"; done || echo "  (none found)"
	@echo ""
	@echo " Tools (tools/):"
	@find tools -maxdepth 2 -name "pyproject.toml" 2>/dev/null | while read f; do echo "  - $$(basename $$(dirname $$f))"; done || echo "  (none found)"
	@echo ""
	@echo "📱 Apps (apps/):"
	@find apps -maxdepth 2 -name "pyproject.toml" 2>/dev/null | while read f; do echo "  - $$(basename $$(dirname $$f))"; done || echo "  (none found)"
	@echo ""
	@echo "🤖 Agents (agents/):"
	@find agents -maxdepth 2 -name "pyproject.toml" 2>/dev/null | while read f; do echo "  - $$(basename $$(dirname $$f))"; done || echo "  (none found)"

help-test:
	@echo "Testing Commands:"
	@echo "  make test          - Run all tests"
	@echo "  make test-libs     - Test all libraries"
	@echo "  make test-config   - Test configuration library"
	@echo "  make test-models   - Test data models library"
	@echo "  make test-paths    - Test path resolution library"
	@echo "  make quick-test    - Run import smoke tests"