# AS0558844 — forward-engineered distributed system.
#
# `make up` is the whole demo. Everything else is for working on it.

PY ?= python3.12
COMPOSE ?= docker compose

.DEFAULT_GOAL := help

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

# --------------------------------------------------------------- the demo ---

.PHONY: up
up: ## Build and start everything, then seed the workbook's Feb 2026 figures
	$(COMPOSE) up --build -d
	@echo
	@echo "  frontend      http://localhost:8080"
	@echo "  gateway API   http://localhost:8000/api/v1"
	@echo "  API docs      http://localhost:8000/docs"
	@echo "  traceability  http://localhost:8000/api/v1/traceability"
	@echo "  RabbitMQ      http://localhost:15672   (as0558844 / as0558844)"
	@echo "  MinIO console http://localhost:9001    (as0558844 / as0558844)"
	@echo
	@echo "  seeding (follow with: make seed-logs)"

.PHONY: down
down: ## Stop everything and drop the volumes
	$(COMPOSE) down -v

.PHONY: logs
logs: ## Tail every service
	$(COMPOSE) logs -f

.PHONY: seed-logs
seed-logs: ## Show the seeder's output, including the equivalence spot-check
	$(COMPOSE) logs seed

.PHONY: reseed
reseed: ## Re-run the one-shot seeder against a running stack
	$(COMPOSE) run --rm seed

.PHONY: ps
ps: ## Show container status
	$(COMPOSE) ps

# ------------------------------------------------------------ development ---

.PHONY: install
install: ## Install the shared package, all seven services and the seeder (editable)
	$(PY) -m pip install --break-system-packages -q -e shared
	$(PY) -m pip install --break-system-packages -q \
	  -e services/workflow -e services/source -e services/risk \
	  -e services/controls -e services/reporting -e services/audit \
	  -e services/gateway -e seed
	cd frontend && npm install

.PHONY: test
test: ## Run every Python suite in one invocation
	$(PY) -m pytest -q

.PHONY: typecheck
typecheck: ## Type-check the React frontend
	cd frontend && npx tsc --noEmit

.PHONY: build-frontend
build-frontend: ## Build the SPA
	cd frontend && npm run build

.PHONY: check
check: test typecheck ## Everything CI would run

# ---------------------------------------------------------- traceability ---

.PHONY: traceability
traceability: ## Roll up all seven registries from the running stack (Phase 5 input)
	@curl -s http://localhost:8000/api/v1/traceability | $(PY) -m json.tool

.PHONY: coverage-check
coverage-check: ## Fail if any of the 59 Asset 4 rules is unregistered
	@$(PY) -c "import json,urllib.request as u; \
d=json.load(u.urlopen('http://localhost:8000/api/v1/traceability')); \
print(f\"{d['registered']}/{d['total']} rules registered\"); \
raise SystemExit(0 if d.get('complete') else 1)"

.PHONY: traceability-matrix
traceability-matrix: ## Regenerate TRACEABILITY.md from the running stack (Phase 5)
	node tools/export_frontend_bindings.mjs
	$(PY) tools/generate_traceability_matrix.py
	$(PY) tools/render_traceability_md.py
