SHELL := /usr/bin/env bash
.DEFAULT_GOAL := help
.NOTPARALLEL: verify

.PHONY: help verify fmt lint test compose-config open-pilot-check catalog dist

help: ## Show available commands
	@awk 'BEGIN {FS = ":.*##"; printf "Commands:\n"} /^[a-zA-Z0-9_-]+:.*##/ {printf "  %-18s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

verify: fmt-check lint test compose-config open-pilot-check ## Run all checks

fmt: ## Format Go files
	@git ls-files -z --cached --others --exclude-standard '*.go' | xargs -0 -r gofmt -w

lint: ## Run Go vet
	go vet ./...

test: ## Run Go tests
	go test ./...

catalog: ## Fetch the pinned theme sources and build .cache/catalog.db.zst
	@mkdir -p .cache
	go run ./cmd/catalog-build -fetch

dist: ## Build release tarballs into dist/ (VERSION=x.y.z; needs builtin/catalog.db.zst)
	@test -n "$(VERSION)" || { echo "usage: make dist VERSION=x.y.z" >&2; exit 1; }
	scripts/dist.sh $(VERSION)

compose-config: ## Validate Docker Compose if present
	@if [ -f docker-compose.yml ]; then docker compose config --quiet; else echo 'skip compose config'; fi

open-pilot-check: ## Check all issue forms and label definitions
	@set -e; for form in task bug feature docs ready; do \
		test -f .gitea/ISSUE_TEMPLATE/open-pilot-$$form.yaml; \
		grep -q 'Test Command' .gitea/ISSUE_TEMPLATE/open-pilot-$$form.yaml; \
	done
	@grep -q 'status:needs-review' .gitea/ISSUE_TEMPLATE/open-pilot-task.yaml
	@! grep -Eq '^ *- ready-for-agent$$|^ *- agent:queued$$' .gitea/ISSUE_TEMPLATE/open-pilot-task.yaml
	@grep -q 'agent:queued' .gitea/ISSUE_TEMPLATE/open-pilot-ready.yaml
	@test -f docs/reference/open-pilot-labels.yaml

.PHONY: fmt-check
fmt-check: ## Check formatting without changing sources
	@set -e; unformatted=$$(git ls-files -z --cached --others --exclude-standard '*.go' | xargs -0 -r gofmt -l); \
	if [ -n "$$unformatted" ]; then printf '%s\n' "$$unformatted"; exit 1; fi
