.PHONY: check check-fmt check-test check-clippy check-lines fmt clippy test addon-image addon-push help

# Run all quality gates (delegates to the shared script for use in CI).
check:
	@./scripts/check.sh

# Individual gates (for iterating on a single failure).
check-fmt:
	cargo fmt --check

check-test:
	cargo test

check-clippy:
	cargo clippy --all-targets -- -D warnings

# Ensure no Rust source file exceeds 500 lines (see AGENTS.md).
check-lines:
	@exit_code=0; \
	max=500; \
	for f in src/**/*.rs src/*.rs; do \
		[ -f "$$f" ] || continue; \
		lines=$$(wc -l < "$$f"); \
		if [ "$$lines" -gt "$$max" ]; then \
			echo "FAIL: $$f has $$lines lines (max $$max)"; \
			exit_code=1; \
		fi; \
	done; \
	if [ "$$exit_code" -eq 0 ]; then \
		echo "✅ All Rust source files are under $$max lines"; \
	else \
		exit "$$exit_code"; \
	fi

# Auto-fix formatting (not a gate; use sparingly before committing).
fmt:
	cargo fmt --all

# The sm121 HA add-on image (docker-images/sm121-addon/): multi-stage
# docker build from the repo root (src/ compiled in-container — the binary
# always matches the container's arch; .dockerignore trims the context).
# Smoke test: scripts/smoke-addon.sh.
addon-image:
	docker build -t sm121-addon:latest -f docker-images/sm121-addon/Dockerfile docker-images/sm121-addon/

# Push the add-on image to the [registry] base (the HA add-on's config.yaml
# references it). Requires `docker login ghcr.io`.
# The image is THIN (no sm121 binary — the entrypoint compiles it on the Pi
# from the launcher repo, so the arch always matches). Built on an aarch64
# host for the Pi; for a multi-arch manifest (x86_64 too) use:
#   docker buildx build --platform linux/aarch64,linux/x86_64 \
#     -t ghcr.io/kilork/sm121-addon:latest --push -f docker-images/sm121-addon/Dockerfile docker-images/sm121-addon/
# (needs qemu binfmt on the builder).
addon-push:
	docker tag sm121-addon:latest ghcr.io/kilork/sm121-addon:latest
	docker push ghcr.io/kilork/sm121-addon:latest

# Auto-fix lints (clippy --fix); review the diff before committing.
clippy-fix:
	cargo clippy --all-targets --fix --allow-dirty --allow-staged

help:
	@echo "make check         Run all quality gates (fmt, test, clippy, lines)"
	@echo "make check-fmt     Run cargo fmt --check"
	@echo "make check-test    Run cargo test"
	@echo "make check-clippy  Run cargo clippy --all-targets -- -D warnings"
	@echo "make check-lines   Verify all src/**/*.rs are ≤500 lines"
	@echo "make fmt           Auto-fix formatting"
	@echo "make clippy-fix    Apply clippy suggestions (review diff)"
