.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.
# The add-on image lives in the sibling sm121-ha-integration repo (the HA
# add-on store: repository.json + sm121/ + the GitHub Actions workflows)
# and is built + pushed THERE by CI (ghcr.io/kilork/sm121-addon, OIDC —
# no docker login). Local build for dev / smoke:
addon-image:
	docker build -t sm121-addon:latest -f ../sm121-ha-integration/sm121/Dockerfile ../sm121-ha-integration/sm121/

# No push target: GitHub Actions (sm121-ha-integration) publishes
# ghcr.io/kilork/sm121-addon on push to main (aarch64 + amd64 + manifest).
# The GHCR package must be set public (first time only) so HA can pull it.

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