# Raic — convenience targets for local development.
# Run `make` or `make help` to see available commands.

.DEFAULT_GOAL := help

CARGO ?= cargo
UV ?= uv

.PHONY: help
help: ## Show this help (default target)
	@printf '\n'
	@printf '  \033[1;36mRaic\033[0m — compile Raic → Rust (`braids` CLI)\n'
	@printf '\n'
	@grep -hE '^[a-zA-Z0-9_.-]+:.*?## ' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[1;33m%-20s\033[0m %s\n", $$1, $$2}'
	@printf '\n'
	@printf '  \033[2mTip: prerequisites for coverage —\033[0m\n'
	@printf '  \033[2m  rustup component add llvm-tools-preview\033[0m\n'
	@printf '  \033[2m  cargo install cargo-llvm-cov\033[0m\n'
	@printf '\n'

# --- Build ---

.PHONY: build
build: ## Build the library (debug)
	$(CARGO) build

.PHONY: release
release: ## Build optimized release artifacts
	$(CARGO) build --release

.PHONY: check
check: ## Fast typecheck without producing binaries
	$(CARGO) check --all-targets

# --- Test ---

.PHONY: test
test: ## Run all tests (unit + integration)
	$(CARGO) test --workspace --all-targets

.PHONY: test-lib
test-lib: ## Run only library unit tests
	$(CARGO) test --lib

.PHONY: test-integration
test-integration: ## Run only integration tests
	$(CARGO) test --test integration

.PHONY: test-quiet
test-quiet: ## Run full suite with quiet output
	$(CARGO) test -q

# --- Quality ---

.PHONY: fmt
fmt: ## Format all Rust code
	$(CARGO) fmt

.PHONY: fmt-check
fmt-check: ## Check formatting (CI-style)
	$(CARGO) fmt --check

.PHONY: clippy
clippy: ## Run Clippy on all targets (warnings allowed)
	$(CARGO) clippy --all-targets --all-features

.PHONY: clippy-strict
clippy-strict: ## Clippy with -D warnings (stricter; may fail until lints are clean)
	$(CARGO) clippy --all-targets --all-features -- -D warnings

.PHONY: lint
lint: fmt-check clippy ## rustfmt --check + Clippy

.PHONY: verify
verify: check test clippy ## Typecheck + full tests + Clippy (no rustfmt gate)

.PHONY: strict
strict: fmt-check verify ## Like verify but requires `cargo fmt` clean tree first

# --- Docs & coverage ---

.PHONY: doc
doc: ## Build API docs (no deps), open in browser
	$(CARGO) doc --no-deps --document-private-items --open

.PHONY: doc-ci
doc-ci: ## Build API docs without opening a browser
	$(CARGO) doc --no-deps --document-private-items

.PHONY: cov
cov: ## LLVM coverage: gate is zero fully uncovered lines (see .cargo/config.toml)
	$(CARGO) cov

.PHONY: cov-strict
cov-strict: ## Coverage with strict 100% line gate (cov-strict alias)
	$(CARGO) cov-strict

# --- Cleanup ---

.PHONY: clean
clean: ## Remove build artifacts (target/)
	$(CARGO) clean

# --- Tokenizer stats (frontier BPE fixtures; CPU-only) ---

.PHONY: venv
venv: ## Sync .venv with uv (pyproject.toml + uv.lock; --no-install-project)
	@$(UV) sync --no-install-project

.PHONY: token-stats
token-stats: venv ## Corpus token counts → target/token_stats.json (runs extract_examples if needed)
	@$(UV) run python scripts/token_stats.py

.PHONY: evolve
evolve: venv ## Score all grammar permutations → target/evolve_results.json
	@$(UV) run python scripts/evolve.py

# --- Syntax-highlighted HTML docs (requires npm) ---

.PHONY: docs-html
docs-html: ## Render README.md & PROMPT.md → docs/*.html (Shiki + TextMate Raic grammar)
	@npm ci
	@npm run docs:html

# --- Meta ---

.PHONY: all
all: build test ## Build debug + run full test suite
