# City Builder - Makefile
.PHONY: help install install-dev run dev test test-verbose test-watch clean lint format check-format check-lint setup venv activate build-requirements freeze-requirements reset-db check-deps

# Default target
help:
	@echo "City Builder - Available make targets:"
	@echo ""
	@echo "Setup:"
	@echo "  venv              Create virtual environment"
	@echo "  install           Install dependencies"
	@echo "  install-dev       Install with dev dependencies"
	@echo "  setup             Complete setup (venv + install)"
	@echo ""
	@echo "Development:"
	@echo "  run               Start the server"
	@echo "  dev               Start server in development mode"
	@echo "  test              Run all tests"
	@echo "  test-verbose      Run tests with verbose output"
	@echo "  test-watch        Run tests in watch mode"
	@echo ""
	@echo "Code Quality:"
	@echo "  lint              Run linting checks"
	@echo "  format            Format code with black"
	@echo "  check-format      Check code formatting"
	@echo "  check-lint        Check linting without fixing"
	@echo ""
	@echo "Database:"
	@echo "  reset-db          Reset the database"
	@echo ""
	@echo "Maintenance:"
	@echo "  clean             Clean up cache files"
	@echo "  freeze-requirements  Update requirements.txt"
	@echo "  check-deps        Check for outdated dependencies"

# Variables
PYTHON = python3
VENV_DIR = .venv
VENV_PYTHON = $(VENV_DIR)/bin/python
VENV_PIP = $(VENV_DIR)/bin/pip
VENV_ACTIVATE = source $(VENV_DIR)/bin/activate

# Create virtual environment
venv:
	$(PYTHON) -m venv $(VENV_DIR)
	@echo "Virtual environment created at $(VENV_DIR)"
	@echo "Activate with: source $(VENV_DIR)/bin/activate"

# Install dependencies
install: $(VENV_DIR)
	$(VENV_PIP) install --upgrade pip
	$(VENV_PIP) install -r requirements.txt

# Install with development dependencies
install-dev: $(VENV_DIR)
	$(VENV_PIP) install --upgrade pip
	$(VENV_PIP) install -r requirements.txt
	$(VENV_PIP) install black flake8 isort pytest-cov mypy

# Complete setup
setup: venv install
	@echo "Setup complete!"
	@echo "To activate the virtual environment, run:"
	@echo "  source $(VENV_DIR)/bin/activate"

# Run the server
run:
	@echo "Starting City Builder server..."
	$(VENV_PYTHON) run.py

# Run in development mode
dev:
	@echo "Starting City Builder in development mode..."
	$(VENV_PYTHON) -m uvicorn server.main:app --host 127.0.0.1 --port 9901 --reload

# Run tests
test:
	$(VENV_PYTHON) -m pytest

# Run tests with verbose output
test-verbose:
	$(VENV_PYTHON) -m pytest -v -s

# Run tests in watch mode (requires pytest-watch)
test-watch:
	$(VENV_PYTHON) -m pytest-watch

# Run specific test file
test-smoke:
	$(VENV_PYTHON) -m pytest tests/test_smoke.py -v

test-integration:
	$(VENV_PYTHON) -m pytest tests/test_integration_*.py -v

test-multiplayer:
	$(VENV_PYTHON) -m pytest tests/test_multiplayer_*.py -v

# Code formatting with black
format:
	@if [ -d "$(VENV_DIR)" ]; then \
		$(VENV_PYTHON) -m black server/ tests/ run.py || echo "black not installed, run 'make install-dev'"; \
	else \
		echo "Virtual environment not found. Run 'make venv' first."; \
	fi

# Check code formatting
check-format:
	@if [ -d "$(VENV_DIR)" ]; then \
		$(VENV_PYTHON) -m black --check server/ tests/ run.py || echo "black not installed, run 'make install-dev'"; \
	else \
		echo "Virtual environment not found. Run 'make venv' first."; \
	fi

# Run linting
lint:
	@if [ -d "$(VENV_DIR)" ]; then \
		$(VENV_PYTHON) -m flake8 server/ tests/ run.py --max-line-length=88 --extend-ignore=E203,W503 || echo "flake8 not installed, run 'make install-dev'"; \
	else \
		echo "Virtual environment not found. Run 'make venv' first."; \
	fi

# Check linting without fixing
check-lint: lint

# Sort imports
sort-imports:
	@if [ -d "$(VENV_DIR)" ]; then \
		$(VENV_PYTHON) -m isort server/ tests/ run.py || echo "isort not installed, run 'make install-dev'"; \
	else \
		echo "Virtual environment not found. Run 'make venv' first."; \
	fi

# Type checking
type-check:
	@if [ -d "$(VENV_DIR)" ]; then \
		$(VENV_PYTHON) -m mypy server/ || echo "mypy not installed, run 'make install-dev'"; \
	else \
		echo "Virtual environment not found. Run 'make venv' first."; \
	fi

# Clean up cache files and temporary files
clean:
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	find . -type f -name ".coverage" -delete
	find . -type d -name ".pytest_cache" -exec rm -rf {} +
	find . -type d -name ".mypy_cache" -exec rm -rf {} +
	@echo "Cleaned up cache files"

# Reset database
reset-db:
	@echo "Resetting database..."
	rm -f data/game.db
	mkdir -p data
	@echo "Database reset complete"

# Freeze current dependencies
freeze-requirements:
	$(VENV_PIP) freeze > requirements-frozen.txt
	@echo "Frozen requirements saved to requirements-frozen.txt"

# Check for outdated dependencies
check-deps:
	$(VENV_PIP) list --outdated

# Build and run in production mode
prod:
	@echo "Starting in production mode..."
	$(VENV_PYTHON) -m uvicorn server.main:app --host 0.0.0.0 --port 9901

# Check virtual environment exists
$(VENV_DIR):
	@if [ ! -d "$(VENV_DIR)" ]; then \
		echo "Virtual environment not found. Creating..."; \
		$(MAKE) venv; \
	fi

# Coverage report
coverage:
	$(VENV_PYTHON) -m pytest --cov=server --cov-report=html --cov-report=term
	@echo "Coverage report generated in htmlcov/"

# Run all quality checks
qa: check-format check-lint type-check test
	@echo "All quality checks passed!"

# Quick development cycle
quick: format test
	@echo "Quick development cycle complete!"