.PHONY: help serve stop test clean install dev test-unit test-int test-e2e lint format watch vm-create vm-list coverage

# Default target
help:
	@echo "Proxym Makefile"
	@echo ""
	@echo "Server commands:"
	@echo "  serve         - Start the proxy server (default port 4000)"
	@echo "  serve-port    - Start on specific port (PORT=4001)"
	@echo "  dev           - Start development server with reload"
	@echo "  stop          - Stop all running proxy servers"
	@echo "  stop-port     - Stop server on specific port (PORT=4001)"
	@echo "  restart       - Restart the server"
	@echo "  status        - Check running processes"
	@echo ""
	@echo "Test commands:"
	@echo "  test          - Run all tests"
	@echo "  test-unit     - Run unit tests only (fast)"
	@echo "  test-int      - Run integration tests"
	@echo "  test-e2e      - Run E2E tests"
	@echo "  test-gui      - Run GUI tests only"
	@echo "  test-live     - Run live tests"
	@echo "  test-watch    - Run tests in watch mode (TDD)"
	@echo "  coverage      - Generate coverage report"
	@echo ""
	@echo "Code quality:"
	@echo "  lint          - Run ruff and mypy checks"
	@echo "  format        - Format code with ruff"
	@echo ""
	@echo "VM commands:"
	@echo "  vm-list       - List all VMs"
	@echo "  vm-create     - Create new VM (interactive)"
	@echo "  vm-start      - Start VM (VM=name)"
	@echo "  vm-stop       - Stop VM (VM=name)"
	@echo ""
	@echo "Other commands:"
	@echo "  install       - Install dependencies"
	@echo "  setup         - Setup development environment"
	@echo "  clean         - Clean cache and temp files"
	@echo "  watch         - Start file watcher for context updates"
	@echo "  metrics       - Generate code metrics"

# Start server
serve:
	@echo "Starting Proxym server..."
	.venv/bin/proxym serve

# Start server on specific port
serve-port:
	@if [ -z "$(PORT)" ]; then echo "Usage: make serve-port PORT=4001"; exit 1; fi
	@echo "Starting Proxym server on port $(PORT)..."
	.venv/bin/proxym serve --port $(PORT)

# Development server with reload
dev:
	@echo "Starting development server with reload..."
	@DEV_PORT=$$(.venv/bin/python -c "from proxym.config import get_settings; print(get_settings().dev_port)"); \
	echo "Stopping server on port $$DEV_PORT..."; \
	fuser -k $$DEV_PORT/tcp 2>/dev/null || true; \
	lsof -ti:$$DEV_PORT | xargs kill -9 2>/dev/null || true; \
	echo "Port $$DEV_PORT cleared"; \
	.venv/bin/proxym serve --reload --port $$DEV_PORT

# Development server (alias)
dev-serve: dev

# Stop all running servers
stop:
	@echo "Stopping all Proxym servers..."
	@pkill -f "proxym serve" || true
	@pkill -f "proxym.*serve" || true
	@pkill -f "python.*proxym" || true
	@echo "Checking for remaining processes..."
	@ps aux | grep -v grep | grep proxym || echo "No Proxym processes found"

# Force stop (kill on port)
stop-port:
	@if [ -z "$(PORT)" ]; then echo "Usage: make stop-port PORT=4001"; exit 1; fi
	@echo "Stopping server on port $(PORT)..."
	@fuser -k $(PORT)/tcp 2>/dev/null || true
	@lsof -ti:$(PORT) | xargs kill -9 2>/dev/null || true
	@echo "Port $(PORT) cleared"

# Check what's running
status:
	@echo "Checking running Proxym processes..."
	@ps aux | grep -v grep | grep proxym || echo "No Proxym processes found"
	@echo ""
	@echo "Checking ports 4000-4002..."
	@netstat -tlnp 2>/dev/null | grep ":400[0-2]" || echo "No servers on ports 4000-4002"

# Run tests
test:
	@echo "Running all tests..."
	.venv/bin/python -m pytest tests/ -v

# Run unit tests only (fast)
test-unit:
	@echo "Running unit tests..."
	.venv/bin/python -m pytest tests/ \
	  --ignore=tests/test_e2e.py \
	  --ignore=tests/test_dashboard_live.py \
	  --ignore=tests/test_dashboard_performance.py \
	  --ignore=tests/gui/ \
	  -q --tb=short -x

# Run integration tests
test-int:
	@echo "Running integration tests..."
	docker compose up -d redis 2>/dev/null || echo "Redis not available, continuing..."
	.venv/bin/python -m pytest tests/test_dashboard_integration.py \
	   tests/test_api_completions.py \
	   tests/test_api_system.py \
	   tests/test_api_context.py \
	   -v --tb=short

# Run E2E tests
test-e2e:
	@echo "Running E2E tests..."
	.venv/bin/python -m pytest tests/test_e2e.py tests/test_dashboard_live.py -v --timeout=300

# Run tests in watch mode (TDD)
test-watch:
	@echo "Running tests in watch mode..."
	.venv/bin/python -m pytest_watch tests/ -- -q --tb=short

# Run GUI tests only
test-gui:
	@echo "Running GUI tests..."
	.venv/bin/python -m pytest tests/gui/ -v

# Run live tests
test-live:
	@echo "Running live tests..."
	.venv/bin/python -m pytest tests/test_dashboard_live.py -v

# Generate coverage report
coverage:
	@echo "Generating coverage report..."
	.venv/bin/python -m pytest tests/ \
	  --ignore=tests/test_e2e.py \
	  --ignore=tests/test_dashboard_live.py \
	  --cov=src/proxym \
	  --cov-report=html \
	  --cov-report=term-missing
	@echo "Coverage report: htmlcov/index.html"

# Run linting
lint:
	@echo "Running linters..."
	.venv/bin/ruff check src/ tests/
	.venv/bin/mypy src/proxym --ignore-missing-imports

# Format code
format:
	@echo "Formatting code..."
	.venv/bin/ruff format src/ tests/

# VM commands
vm-list:
	@echo "Listing VMs..."
	.venv/bin/proxym vm list

vm-create:
	@read -p "VM name: " name; \
	 read -p "Tool (windsurf/cursor/vscode): " tool; \
	 read -p "Account ID: " account; \
	 .venv/bin/proxym vm create --name $$name --tool $$tool --account $$account

vm-start:
	@if [ -z "$(VM)" ]; then echo "Usage: make vm-start VM=windsurf-work"; exit 1; fi
	.venv/bin/proxym vm start $(VM)

vm-stop:
	@if [ -z "$(VM)" ]; then echo "Usage: make vm-stop VM=windsurf-work"; exit 1; fi
	.venv/bin/proxym vm stop $(VM)

# Start file watcher for context updates
watch:
	@echo "Starting file watcher..."
	.venv/bin/proxym watch $(PWD) --push-to http://localhost:4000

# Generate code metrics
metrics:
	@echo "Generating code metrics..."
	@which code2llm >/dev/null 2>&1 && code2llm ./ -f all -o ./project --no-chunk || echo "code2llm not installed"
	@echo "Metrics in ./project/"

# Install dependencies
install:
	@echo "Installing dependencies..."
	python -m venv .venv || true
	.venv/bin/pip install -e ".[dev]"
	.venv/bin/playwright install || true

# Clean cache and temp files
clean:
	@echo "Cleaning cache..."
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	rm -rf .pytest_cache 2>/dev/null || true
	rm -rf .coverage 2>/dev/null || true
	rm -rf htmlcov 2>/dev/null || true
	@echo "Cache cleaned"

# Quick restart
restart: stop serve

# Development setup
setup: install
	@echo "Setup complete. Use 'make dev' to start development server."
