DEFAULT_BACKEND ?= bls-backend-blst
DEFAULT_FEATURES ?= $(DEFAULT_BACKEND)
FULL_FEATURES ?= legacy-proving,debug,rkyv-impl,zeroize,rkyv/size_32,$(DEFAULT_BACKEND)
NO_STD_FEATURES ?= alloc,$(DEFAULT_BACKEND)

help: ## Display this help screen
	@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

build: ## Build library
	@cargo build --features $(DEFAULT_FEATURES)

test: ## Run tests
	@cargo test --release --features $(DEFAULT_FEATURES)
	@cargo test --release --features $(FULL_FEATURES)

clippy: ## Run clippy
	@cargo clippy --features $(FULL_FEATURES) -- -D warnings
	@cargo clippy --no-default-features --features $(NO_STD_FEATURES) -- -D warnings

cq: ## Run code quality checks (formatting + clippy)
	@$(MAKE) fmt CHECK=1
	@$(MAKE) clippy

fmt: ## Format code (requires nightly)
	@rustup component add --toolchain nightly rustfmt 2>/dev/null || true
	@cargo +nightly fmt --all $(if $(CHECK),-- --check,)

bench: ## Run benchmarks
	@cargo bench --features $(DEFAULT_FEATURES)

build-benches: ## Build benchmarks without running
	@cargo bench --no-run --features $(DEFAULT_FEATURES)

examples: ## Run examples
	@cargo run --release --example circuit --features $(DEFAULT_FEATURES)

no-std: ## Verify no_std compatibility
	@rustup target add thumbv6m-none-eabi
	@cargo build --release --no-default-features --features $(NO_STD_FEATURES) --target thumbv6m-none-eabi
	@cargo build --release --no-default-features --features $(DEFAULT_BACKEND) --target thumbv6m-none-eabi

doc: ## Generate documentation
	@cargo rustdoc --lib --features $(DEFAULT_FEATURES) -- --html-in-header katex-header.html -D warnings

doc-internal: ## Generate documentation with private items
	@cargo rustdoc --lib --features $(DEFAULT_FEATURES) -- --document-private-items -D warnings

doc-local: ## Open local documentation
	@RUSTDOCFLAGS="--html-in-header katex-header.html" cargo doc --no-deps --open --features $(DEFAULT_FEATURES)

clean: ## Clean build artifacts
	@cargo clean

.PHONY: help build test clippy cq fmt bench build-benches examples no-std doc doc-internal doc-local clean
