.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 (the sibling sm121-ha-integration repo is the
# HA add-on store: repository.json + sm121/ + the GitHub Actions
# workflows). Multi-stage: rust:alpine compiles sm121 from launcher-src/
# (this repo, rsynced into the build context) with a trivial
# `cargo build --release` (pinned rust:alpine, native arch, dynamic musl);
# the alpine final image ships it (~25 MB — no toolchain, no
# compile-on-boot: a 4 GB Pi must not OOM on a release build).
# Smoke test: scripts/smoke-addon.sh.
# CI (sm121-ha-integration) builds + publishes ghcr.io/kilork/sm121-addon
# (aarch64 + amd64 + multi-arch manifest, OIDC — no docker login) on
# push to main. The GHCR package must be set public (first time only).
# Local build for dev / smoke:
addon-image:
	rm -rf ../sm121-ha-integration/launcher-src
	rsync -a --exclude .git --exclude target ./ ../sm121-ha-integration/launcher-src/
	docker build -t sm121-addon:latest -f ../sm121-ha-integration/sm121/Dockerfile ../sm121-ha-integration/

# 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)"
