# BlackRoad OS — Pave Tomorrow.
.PHONY: help sync push health fleet workers test lint

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

sync: ## Sync all sources into monorepo
	@./sync.sh

push: ## Sync and push to Gitea
	@./sync.sh push

health: ## Check fleet health
	@echo "Checking workers..."
	@for w in roadcode-squad squad-webhook stats-blackroad analytics-blackroad fleet-dashboard; do \
		code=$$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "https://$$w.amundsonalexa.workers.dev/health" 2>/dev/null); \
		if [ "$$code" = "200" ]; then echo "  ✅ $$w"; else echo "  ❌ $$w ($$code)"; fi; \
	done
	@for w in roadpay road-search blackroad-stripe; do \
		code=$$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "https://$$w.amundsonalexa.workers.dev/health" 2>/dev/null); \
		if [ "$$code" = "200" ]; then echo "  ✅ $$w"; else echo "  ❌ $$w ($$code)"; fi; \
	done

fleet: ## Check fleet node status
	@echo "Pinging nodes..."
	@for node in "Alice:192.168.4.49" "Cecilia:192.168.4.96" "Octavia:192.168.4.101" "Aria:192.168.4.98" "Lucidia:192.168.4.38"; do \
		name=$${node%%:*}; ip=$${node##*:}; \
		if ping -c 1 -W 2 $$ip > /dev/null 2>&1; then echo "  ✅ $$name ($$ip)"; else echo "  ❌ $$name ($$ip)"; fi; \
	done

workers: ## List all worker sources
	@echo "Workers in monorepo:"
	@ls -d workers/*-src/ 2>/dev/null | sed 's|workers/||;s|-src/||' | while read w; do echo "  $$w"; done

test: ## Run tests (language auto-detect)
	@if [ -f package.json ]; then npm test 2>/dev/null || true; fi
	@if [ -f requirements.txt ]; then python -m pytest 2>/dev/null || true; fi
	@find scripts/ -name '*.sh' -exec bash -n {} \; 2>&1 | head -20 && echo "  ✅ Shell scripts syntax OK"

lint: ## Lint everything
	@echo "Linting shell scripts..."
	@find scripts/ bin/ -name '*.sh' -exec shellcheck -S warning {} \; 2>&1 | head -30 || true
	@echo "Done."
