# Root Makefile — cross-cutting targets only.
#
# Block packaging: per-block Makefiles are the canonical API (see
# packaging/block.mk). Invoke them directly, or loop from a shell:
#
#   # build one .deb for every block that has a packaging/ scaffold
#   for b in $(make pkg-list-blocks -s); do make -C $$b pkg-deb; done
#
#   # full matrix for every block
#   for b in $(make pkg-list-blocks -s); do make -C $$b pkg-all; done

.PHONY: help pkg-list-blocks pkg-clean \
        system-test system-test-keep system-test-down system-test-logs

PACKAGED_BLOCKS := $(shell find blocks -type d -name packaging -exec dirname {} \; 2>/dev/null | sort)

help:
	@echo "Cross-cutting:"
	@echo "  make pkg-list-blocks     list blocks with a packaging/ scaffold"
	@echo "  make pkg-clean           wipe dist/packaging/"
	@echo ""
	@echo "System (E2E) tests:"
	@echo "  make system-test         build stack, run tests, tear down"
	@echo "  make system-test-keep    build stack, run tests, LEAVE RUNNING for inspection"
	@echo "  make system-test-fast    run tests against already-running stack"
	@echo "  make system-test-down    tear down the system test stack"
	@echo "  make system-test-logs    tail logs from all system test services"
	@echo ""
	@echo "Per-block — see 'make help' inside each block's directory."

# Prints one block path per line — pipe-friendly:
#   for b in $(make pkg-list-blocks -s); do make -C $$b pkg-deb; done
pkg-list-blocks:
	@for b in $(PACKAGED_BLOCKS); do echo $$b; done

pkg-clean:
	@rm -rf dist/packaging/
	@echo "cleaned dist/packaging/"

# ── System (E2E) Tests ───────────────────────────────────────────────────────
SYSTEM_COMPOSE  := tests/system/docker-compose.yml
SYSTEM_PROJECT  := edgebits-systest
SYSTEM_NETWORK  := $(SYSTEM_PROJECT)_system-test
COMPOSE_SYSTEST := docker compose -f $(SYSTEM_COMPOSE) --project-name $(SYSTEM_PROJECT)

# Internal: run pytest inside a Python container on the system-test network.
# --skip-start tells conftest not to call docker compose (Makefile owns lifecycle).
# EXTRA_ARGS can pass --no-teardown or any pytest flags.
_run-pytest:
	docker run --rm \
	  --network $(SYSTEM_NETWORK) \
	  -v "$(CURDIR)/tests/system:/tests:ro" \
	  -e EDGE_URL=http://core:8080 \
	  -e MANAGER_URL=http://edge-manager-api:9000 \
	  -e DB_URL=postgres://edgebits:edgebits@postgres:5432/edgebits \
	  python:3.13-slim \
	  sh -c "pip install -q requests psycopg2-binary pytest && cd /tests && pytest . -v --skip-start $(EXTRA_ARGS)"

# Run full suite and tear down when done (always starts clean)
system-test: system-test-down _system-up
	$(MAKE) _run-pytest EXTRA_ARGS=""
	$(MAKE) system-test-down

# Run full suite, LEAVE stack running for manual inspection (always starts clean)
system-test-keep: system-test-down _system-up
	$(MAKE) _run-pytest EXTRA_ARGS="--no-teardown"
	@echo ""
	@echo "Stack is still running:"
	@echo "  Edge Manager UI   http://localhost:4007"
	@echo "  Edge Manager API  http://localhost:9007/api/v1"
	@echo "  Edge Gateway      http://localhost:8087/api/v1"
	@echo "  Edge UI           http://localhost:3007"
	@echo "  psql postgres://edgebits:edgebits@localhost:5440/edgebits"
	@echo ""
	@echo "  make system-test-down   to tear down"

# Run tests against already-running stack (no rebuild)
system-test-fast:
	$(MAKE) _run-pytest EXTRA_ARGS="--no-teardown"

_system-up:
	$(COMPOSE_SYSTEST) up --build --force-recreate -d

system-test-down:
	$(COMPOSE_SYSTEST) down -v

system-test-logs:
	$(COMPOSE_SYSTEST) logs -f
