.PHONY: help build install develop test clean lint format check-fmt benchmark example download-assets

# Default target
help:
	@echo "Available targets:"
	@echo "  build      - Build the Rust extension"
	@echo "  install    - Install the package"
	@echo "  develop    - Install in development mode"
	@echo "  test       - Run tests"
	@echo "  clean      - Clean build artifacts"
	@echo "  lint       - Run linting tools"
	@echo "  format     - Format code"
	@echo "  check-fmt  - Check code formatting"
	@echo "  benchmark  - Run benchmarks"
	@echo "  example    - Run basic usage example"
	@echo "  download-assets - Download test assets from Project Gutenberg"

# Build the extension
build:
	maturin build --release

# Install the package
install: build
	pip install target/wheels/*.whl --force-reinstall

# Development installation
develop:
	maturin develop

# Download test assets if missing
download-assets:
	@mkdir -p tests/assets
	@if [ ! -f tests/assets/pg2600.txt ]; then \
		echo "Downloading War and Peace from Project Gutenberg..."; \
		curl -s -o tests/assets/pg2600.txt https://www.gutenberg.org/cache/epub/2600/pg2600.txt || \
		wget -q -O tests/assets/pg2600.txt https://www.gutenberg.org/cache/epub/2600/pg2600.txt || \
		{ echo "Error: Could not download pg2600.txt. Please install curl or wget."; exit 1; }; \
		echo "Downloaded tests/assets/pg2600.txt"; \
	else \
		echo "tests/assets/pg2600.txt already exists"; \
	fi

# Run tests
test: download-assets
	pytest tests/ -v

# Clean build artifacts
clean:
	cargo clean
	rm -rf target/
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info/
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -name "*.pyc" -delete

# Linting
lint:
	cargo clippy -- -D warnings
	ruff check python/ examples/ tests/
	mypy python/

# Format code
format:
	cargo fmt
	ruff format python/ examples/ tests/

# Check formatting
check-fmt:
	cargo fmt --check
	ruff check python/ examples/ tests/
	ruff format --check python/ examples/ tests/

# Run benchmarks
benchmark: develop
	python -m pytest tests/ -k benchmark -v

# Run basic example
example: develop
	python examples/basic_usage.py

# Publish to PyPI (requires authentication)
publish:
	maturin publish

# Build wheels for multiple platforms
build-wheels:
	cibuildwheel --platform linux

# Create virtual environment
venv:
	python -m venv venv
	./venv/bin/pip install --upgrade pip
	./venv/bin/pip install maturin[patchelf]

# Install development dependencies
dev-deps:
	pip install maturin[patchelf] pytest pytest-benchmark hypothesis ruff mypy pre-commit

# Setup pre-commit hooks
setup-hooks:
	pre-commit install

# Full development setup
setup: venv dev-deps setup-hooks
	@echo "Development environment setup complete!"
	@echo "Activate with: source venv/bin/activate"
	@echo "Build with: make develop"