# SPDX-License-Identifier: Apache-2.0 OR MIT

# Makefile using cargo for managing builds and dependencies in a Rust project.

# Default target executed when no arguments are given to make.
.PHONY: all
all: help ## Display this help.

# One-command bootstrap. Detects the host OS, installs missing toolchain
# components via rustup, and wires up the project's git hooks. Re-running
# this target on a fresh clone is a no-op for anything already in place.
.PHONY: init
init: ## Bootstrap a clean clone (rustfmt + clippy + hooks + first build).
	@echo "==> Detecting platform..."
	@printf "    host: " ; uname -s
	@command -v rustup >/dev/null 2>&1 || { \
	    echo "ERROR: rustup not found. Install from https://rustup.rs/"; \
	    exit 1; \
	}
	@rustup show active-toolchain
	@rustup component add rustfmt clippy 2>/dev/null || true
	@command -v cargo-deny >/dev/null 2>&1 || cargo install cargo-deny
	@$(MAKE) -s hooks
	@cargo build --quiet
	@echo "==> Bootstrap complete. Run 'make test' to verify."

# Install the project-managed git hooks (signed-commit guard, etc).
.PHONY: hooks
hooks: ## Install the project's git hooks under .githooks/.
	@git config core.hooksPath .githooks
	@chmod +x .githooks/pre-commit 2>/dev/null || true
	@echo "✓ Git hooks installed (.githooks/)."

# Run the Criterion benchmark suite.
.PHONY: bench
bench: ## Run performance benchmarks (Criterion).
	@echo "Running benchmarks..."
	@cargo bench --bench bench

# Run llvm-cov with profraw files pinned under target/coverage/ so they
# never leak into the working tree. Mirrors the ci.yml coverage job
# exactly so local + CI numbers match. Uses --lib (not --tests) so the
# heavy example_outputs integration suite stays in its own CI job.
.PHONY: coverage
coverage: ## Run cargo llvm-cov --lib; profraw output stays under target/coverage/.
	@mkdir -p target/coverage
	@LLVM_PROFILE_FILE="$$PWD/target/coverage/%m-%p.profraw" \
	  cargo llvm-cov --lib --features test-fault-injection \
	    --ignore-filename-regex 'src/core/deploy_adapter\.rs|src/main\.rs' \
	    --summary-only

# Enforce the WASM gzipped-size budget (#546 AC10) — Edge payload ≤ 2 MB.
.PHONY: wasm-size
wasm-size: ## Build crates/ssg-wasm and assert it stays ≤ 2 MB gzipped.
	@scripts/wasm-size-gate.sh

# Run automated accessibility checks against generated HTML.
.PHONY: a11y
a11y: build ## Run axe-core WCAG 2.1 AA audit on example site.
	@echo "Generating example site..."
	@cargo run --release -- -c examples/content/en -o /tmp/ssg-a11y -t examples/templates
	@echo "Running axe-core (WCAG 2.1 AA)..."
	@npm --prefix tests/visual install --no-audit --no-fund --silent
	@python3 -m http.server 8787 --directory /tmp/ssg-a11y > /dev/null 2>&1 & \
		SERVER_PID=$$!; \
		sleep 2; \
		node tests/visual/audit-site.mjs --base http://localhost:8787 --dir /tmp/ssg-a11y; \
		rc=$$?; \
		kill $$SERVER_PID 2>/dev/null || true; \
		exit $$rc

# Generate and open API documentation locally.
.PHONY: doc
doc: ## Generate API docs and open in browser.
	@echo "Generating API documentation..."
	@RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p ssg --open

# Build the project including all workspace members.
.PHONY: build
build: ## Build the project.
	@echo "Building all project components..."
	@cargo build --all

# Remove build artifacts and stray logs from the working tree.
.PHONY: clean
clean: ## Remove build artifacts and stray logs.
	@echo "Cleaning build artifacts..."
	@cargo clean
	@rm -rf examples/build examples/public public/ build/
	@rm -f site.log site_generation.log
	@echo "Done."

# Lint the project with stringent rules using Clippy, install Clippy if not present.
.PHONY: lint
lint: ensure-clippy ## Lint the project with Clippy.
	@echo "Linting with Clippy..."
	@cargo clippy --all-features --all-targets --all -- \
		--deny clippy::dbg_macro --deny clippy::unimplemented --deny clippy::todo --deny warnings \
		--deny missing_docs --deny rustdoc::broken_intra_doc_links --forbid unused_must_use --deny clippy::result_unit_err

# Run all unit and integration tests in the project.
.PHONY: test
test: ## Run tests for the project.
	@echo "Running tests..."
	@cargo test

# Check the project for errors without producing outputs.
.PHONY: check
check: ## Check the project for errors without producing outputs.
	@echo "Checking code formatting..."
	@cargo check

# Format all code in the project according to rustfmt's standards, install rustfmt if not present.
.PHONY: format
format: ensure-rustfmt ## Format the code.
	@echo "Formatting all project components..."
	@cargo fmt --all

# Check code formatting without making changes, with verbose output, install rustfmt if not present.
.PHONY: format-check-verbose
format-check-verbose: ensure-rustfmt ## Check code formatting with verbose output.
	@echo "Checking code format with verbose output..."
	@cargo fmt --all -- --check --verbose

# Apply fixes to the project using cargo fix, install cargo-fix if necessary.
.PHONY: fix
fix: ensure-cargo-fix ## Automatically fix Rust lint warnings using cargo fix.
	@echo "Applying cargo fix..."
	@cargo fix --all

# Use cargo-deny to check for security vulnerabilities, licensing issues, and more, install if not present.
.PHONY: deny
deny: ensure-cargo-deny ## Run cargo deny checks.
	@echo "Running cargo deny checks..."
	@cargo deny check

# Check for outdated dependencies only for the root package, install cargo-outdated if necessary.
.PHONY: outdated
outdated: ensure-cargo-outdated ## Check for outdated dependencies for the root package only.
	@echo "Checking for outdated dependencies..."
	@cargo outdated --root-deps-only

# Installation checks and setups
.PHONY: ensure-clippy ensure-rustfmt ensure-cargo-fix ensure-cargo-deny ensure-cargo-outdated
ensure-clippy:
	@cargo clippy --version || rustup component add clippy

ensure-rustfmt:
	@cargo fmt --version || rustup component add rustfmt

ensure-cargo-fix:
	@cargo version > /dev/null 2>&1 || (echo "cargo is required" && exit 1)

ensure-cargo-deny:
	@command -v cargo-deny || cargo install cargo-deny

ensure-cargo-outdated:
	@command -v cargo-outdated || cargo install cargo-outdated

# Visual regression tests (Playwright)
.PHONY: visual
visual: build ## Run visual regression tests against example site.
	@cd tests/visual && npm ci && npx playwright install --with-deps chromium webkit
	@cargo run -- -c examples/content/en -o examples/public -t examples/templates/en
	@cd tests/visual && npx playwright test

.PHONY: visual-update
visual-update: build ## Update visual regression baselines.
	@cd tests/visual && npm ci && npx playwright install --with-deps chromium webkit
	@cargo run -- -c examples/content/en -o examples/public -t examples/templates/en
	@cd tests/visual && npx playwright test --update-snapshots

# Asserts the README's numeric claims still match the code that produces
# them: plugin count from the registered pipeline, audit-gate count from the
# runner, version from Cargo.toml. Named separately from `test` so a failure
# reads as "the documentation drifted", not "a feature broke".
.PHONY: readme-check
readme-check: ## Verify README counts match the code.
	@cargo test --test readme_sync

# Help target to display callable targets and their descriptions.
.PHONY: help
help: ## Display this help.
	@echo "Usage: make [target]..."
	@echo ""
	@echo "Targets:"
	@awk 'BEGIN {FS = ":.*?##"} /^[a-zA-Z_-]+:.*?##/ {printf "  %-30s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
