# 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 declares a manifest
#   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-node pkg-runtime pkg-event-engine pkg-edge-sync pkg-gateway \
        pkg-gateway-test pkg-node-upgrade-test pkg-manager pkg-manager-test \
        pkg-analytics pkg-analytics-test pkg-analytics-upgrade-test pkg-handover-test \
        pkg-list-blocks pkg-test pkg-clean \
        system-test system-test-keep system-test-down system-test-logs

# A block is packageable because it declares a manifest, NOT because it ships a
# packaging/ directory -- packaging is rendered from the manifest now, so almost
# no block has that directory any more. Keying on it here enumerated 2 of 48.
PACKAGED_BLOCKS := $(shell find blocks -name manifest.json ! -path '*/packaging/*' -exec dirname {} \; 2>/dev/null | sort)

help:
	@echo "Cross-cutting:"
	@echo "  make pkg-node [ARCH=]       build the Edge Node: runtime + event-engine + edge-sync + gateway"
	@echo "  make pkg-gateway [ARCH=]    just the gateway deb (siblings: pkg-runtime, pkg-event-engine, pkg-edge-sync)"
	@echo "  make pkg-manager [ARCH=]    build Edge Manager (server + cli)"
	@echo "  make pkg-analytics [ARCH=]  build Edge Analytics (needs pkg-runtime + pkg-event-engine to install)"
	@echo "  make pkg-gateway-test       install the node set into a bare systemd host and prove it works"
	@echo "  make pkg-manager-test / pkg-analytics-test   the same for the other two products"
	@echo "  make pkg-node-upgrade-test OLD_DEB=...       pre-split single package -> the four-package node"
	@echo "  make pkg-analytics-upgrade-test OLD_DEB=...   the same for Edge Analytics"
	@echo "  make pkg-handover-test      pre-split -> split handover, old shape synthesized (no OLD_DEB needed)"
	@echo "  make pkg-list-blocks     list every packageable block"
	@echo "  make pkg-test            packaging tests that need no build (seconds)"
	@echo "  make pkg-clean           wipe packaging/dist/"
	@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 packaging/dist/
	@echo "cleaned packaging/dist/"

# ── 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

# Packaging tests that need neither a built package nor Docker-in-Docker, so
# they are cheap enough to run before every commit. The install/smoke tests
# need artifacts and live under packaging/testing/ too -- see its README.
pkg-test:
	@bash packaging/testing/render-test.sh
	@bash packaging/testing/noarch-guard-test.sh
	@bash packaging/testing/image-naming-test.sh
	@bash packaging/testing/env-template-test.sh

# The Edge Node is FOUR packages apt resolves together: the shared Python
# runtime, the event engine, Edge Sync and the gateway. `pkg-node` builds the
# set; the per-package targets exist for iterating on one of them.
ARCH ?= arm64
pkg-runtime:
	@bash packaging/python-runtime/build.sh $(ARCH) noble

# Architecture: all -- pure Python on the runtime's interpreter, one deb for both arches.
pkg-event-engine:
	@bash edge/event-engine/packaging/build.sh noble

pkg-edge-sync:
	@bash edge/edge-sync/packaging/build.sh $(ARCH) noble

pkg-gateway:
	@bash edge/packaging/build.sh $(ARCH) noble

pkg-node: pkg-runtime pkg-event-engine pkg-edge-sync pkg-gateway

pkg-gateway-test:
	@bash packaging/testing/gateway-install-test.sh $(ARCH)

# The upgrades from the pre-split single packages. Each needs an older deb, which
# no current build produces -- build one from a checkout of the commit before the
# split: OLD_DEB=packaging/dist/<arch>/edgebits-{edge-gateway,analytics}_<pre-split>.deb
pkg-node-upgrade-test:
	@bash packaging/testing/gateway-split-upgrade-test.sh "$(OLD_DEB)" $(ARCH)

pkg-analytics-upgrade-test:
	@bash packaging/testing/analytics-split-upgrade-test.sh "$(OLD_DEB)" $(ARCH)

# The pre-split -> split handover, with the old shape SYNTHESIZED, so the
# Replaces:/orphan/symlink guarantees are checked on every build. Needs no
# pre-split deb, unlike the two upgrade tests above.
pkg-handover-test:
	@bash packaging/testing/split-handover-test.sh $(ARCH)

# The Edge Manager: API, migrations, catalogue and UI in one package,
# plus edgebits-edge-manager-cli for operator workstations.
pkg-manager:
	@bash edge-manager/packaging/build.sh $(ARCH) noble

pkg-manager-test:
	@bash packaging/testing/manager-install-test.sh $(ARCH)

# The Edge Analytics: API, migrations and UI. The alert engine and the Python
# runtime come from edgebits-event-engine and edgebits-python-runtime (pkg-node).
pkg-analytics:
	@bash edge-analytics/packaging/build.sh $(ARCH) noble

pkg-analytics-test:
	@bash packaging/testing/analytics-install-test.sh $(ARCH)
