.PHONY: install install-full discover reinstall uninstall clean test help list-packages verify

VENV := .venv
PYTHON := $(VENV)/bin/python
UV := uv

# Installation targets (UV Workspace)
install:  ## Install all workspace packages
	@echo "Installing Organon workspace..."
	$(UV) sync --all-packages

install-full:  ## Install with all optional dependencies
	@echo "Installing Organon workspace with full dependencies..."
	$(UV) sync --all-packages --all-extras

discover:  ## Show what would be installed (dry-run)
	@echo "Discovering workspace packages..."
	$(UV) sync --dry-run

reinstall:  ## Force reinstall all packages
	@echo "Force reinstalling workspace..."
	rm -rf $(VENV) uv.lock
	$(UV) sync --all-packages

# Uninstall targets
uninstall:  ## Uninstall all organon packages
	@echo "Uninstalling organon packages..."
	$(UV) pip uninstall -y organon-cli codebase-scanner 2>/dev/null || true

# Development targets
clean:  ## Clean build artifacts and caches
	@echo "Cleaning build artifacts..."
	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 "build" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "dist" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name ".organon_index.json" -delete 2>/dev/null || true

clean-all: clean  ## Clean everything including venv and lockfile
	@echo "Removing virtual environment and lockfile..."
	rm -rf $(VENV) uv.lock

# Testing targets (using UV workspace)
test:  ## Run tests for all tools
	@echo "Running tests for all tools..."
	@for pkg in tools/*/; do \
		if [ -f "$$pkg/pyproject.toml" ]; then \
			pkg_name=$$(basename "$$pkg"); \
			if [ -d "$$pkg/tests" ]; then \
				echo "Testing $$pkg_name..."; \
				cd "$$pkg" && $(UV) run --all-extras pytest -v || exit 1; \
				cd - > /dev/null; \
			else \
				echo "  No tests found for $$pkg_name"; \
			fi; \
		fi; \
	done

# Utility targets
check-plugins:  ## Show installed organon plugins
	@echo "Installed plugins:"
	@$(UV) run python -c "from importlib import metadata; print('\n'.join(f'  - {ep.name}: {ep.value}' for ep in metadata.entry_points().select(group='organon.plugins')))" || echo "  No plugins found"

list-packages:  ## List all workspace packages
	@echo "Discovered Workspace Packages:"
	@echo "=============================="
	@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)"

verify:  ## Verify installation
	@echo "Verifying installation..."
	@echo "Virtual environment: $(VENV)"
	@test -d "$(VENV)" && echo "✓ Virtual environment exists" || echo "✗ Virtual environment not found"
	@test -f "$(VENV)/bin/organon" && echo "✓ organon command found" || echo "✗ organon command not found"
	@test -f "$(VENV)/bin/org" && echo "✓ org command found" || echo "✗ org command not found"
	@test -f "$(VENV)/bin/codebase-scanner" && echo "✓ codebase-scanner command found" || echo "✗ codebase-scanner command not found"
	@test -f "$(VENV)/bin/cb-scan" && echo "✓ cb-scan command found" || echo "✗ cb-scan command not found"
	@echo ""
	@echo "To use organon CLI:"
	@echo "  source $(VENV)/bin/activate"
	@echo "  organon --help"

help:  ## Show this help message
	@echo "Organon Workspace Makefile"
	@echo ""
	@echo "Usage: make [target]"
	@echo ""
	@echo "Installation (UV Workspace):"
	@echo "  install       Install all workspace packages (uv sync)"
	@echo "  install-full  Install with all optional dependencies"
	@echo "  discover      Show what would be installed (dry-run)"
	@echo "  list-packages List all discovered packages"
	@echo "  reinstall     Force reinstall all packages"
	@echo ""
	@echo "Testing:"
	@echo "  test          Run tests for all tools"
	@echo ""
	@echo "Development:"
	@echo "  clean         Clean build artifacts and caches"
	@echo "  clean-all     Clean everything including venv"
	@echo "  verify        Verify installation"
	@echo "  check-plugins Show installed organon plugins"
	@echo ""
	@echo "Utility:"
	@echo "  help          Show this help message"