LPK_FILE ?= noblack.lpk
LPK_MAX_BYTES ?= 12000000

.PHONY: all doctor lint backend-test verify check-lpk build install release-prep clean

all: build

doctor:
	@command -v go >/dev/null || (echo "Error: go is not installed" && exit 1)
	@command -v lzc-cli >/dev/null || (echo "Error: lzc-cli is not installed" && exit 1)
	@echo "Go: $$(go version)"
	@echo "Lazycat CLI: $$(lzc-cli --version)"

lint:
	lzc-cli project lint -f lzc-build.yml

backend-test:
	go test ./...

verify: backend-test lint
	@test "$$(git diff --name-only d5bfbf2c03ecb5e6586f50418474470060786c85 -- cmd internal | wc -l | tr -d ' ')" = "0" || (echo "Error: upstream business source changed" && exit 1)
	@! grep -Eq '(^|[[:space:]])embed:' lzc-manifest.yml lzc-build.yml || (echo "Error: embedded image reference found" && exit 1)
	@! grep -Eq '^images:' lzc-build.yml || (echo "Error: lzc-build.yml images block is forbidden" && exit 1)
	@echo "Packaging boundary checks passed"

check-lpk:
	@test -f "$(LPK_FILE)" || (echo "Error: LPK not found: $(LPK_FILE)" && exit 1)
	@SIZE=$$(wc -c < "$(LPK_FILE)" | tr -d ' '); test "$$SIZE" -le "$(LPK_MAX_BYTES)" || (echo "Error: $(LPK_FILE) is $$SIZE bytes, exceeds $(LPK_MAX_BYTES)" && exit 1); echo "LPK size OK: $$SIZE bytes"
	@TMP_FILE=$$(mktemp); if tar -tf "$(LPK_FILE)" > "$$TMP_FILE" 2>/dev/null; then :; elif unzip -Z1 "$(LPK_FILE)" > "$$TMP_FILE" 2>/dev/null; then :; else echo "Error: cannot list LPK archive contents"; rm -f "$$TMP_FILE"; exit 1; fi; ! grep -Eq '(^|/)images/|(^|/)images.lock$$' "$$TMP_FILE" || (echo "Error: embedded image artifacts found"; rm -f "$$TMP_FILE"; exit 1); rm -f "$$TMP_FILE"; echo "LPK embedded-image check passed"
	lzc-cli lpk info "$(LPK_FILE)"

build: verify
	lzc-cli project release -f lzc-build.yml -o "$(LPK_FILE)"
	@$(MAKE) check-lpk

install: build
	lzc-cli app install "$(LPK_FILE)" --apk n

release-prep: build
	@test -f docs/release-prep/store-listing.md
	@echo "Release package and store materials are ready for runtime screenshots"

clean:
	rm -rf dist build "$(LPK_FILE)"
