# StemEngine backend — development helpers
#
# All commands run tests inside the built Docker image so they use the same
# Python + dependency versions as production.  Build the image first with
# `make build` before running any test targets.

IMAGE   := stemengine:latest
BACKEND := backend

.PHONY: build test test-all test-smoke test-phase4 test-phase5 test-normalization test-findings34 test-findings345 test-finding5 test-hardening alembic-upgrade alembic-heads help

help:
	@echo ""
	@echo "  make build              Build the Docker image"
	@echo "  make test               Run the full backend test suite"
	@echo "  make test-smoke         Smoke tests only (app boot + core routes)"
	@echo "  make test-phase4        Phase 4 operational-hardening tests"
	@echo "  make test-phase5        Phase 5 normalization tests"
	@echo "  make test-normalization Alias for test-phase5"
	@echo "  make test-findings345   Run targeted findings 3/4/5 + optional hardening regressions via compose profile"
	@echo "  make test-findings34    Alias for test-findings345 (backward compatibility)"
	@echo "  make test-hardening     Alias for test-findings345"
	@echo "  make test-finding5      Run targeted finding 5 ensemble regressions via compose profile"
	@echo "  make alembic-upgrade    Apply pending Alembic migrations inside container"
	@echo "  make alembic-heads      Show current Alembic revision heads"
	@echo ""

build:
	docker build -t $(IMAGE) .

# Full test suite — discovers all test_*.py under /app/backend/tests
test:
	docker run --rm $(IMAGE) bash -lc \
	  "python3.11 -m unittest discover -s /app/$(BACKEND)/tests -p 'test_*.py' -v"

# Subset targets for quick iteration
test-smoke:
	docker run --rm $(IMAGE) bash -lc \
	  "python3.11 -m unittest $(BACKEND).tests.test_app_smoke -v"

test-phase4:
	docker run --rm $(IMAGE) bash -lc \
	  "python3.11 -m unittest $(BACKEND).tests.test_operational_hardening -v"

test-phase5 test-normalization:
	docker run --rm $(IMAGE) bash -lc \
	  "python3.11 -m unittest $(BACKEND).tests.test_phase5_normalization -v"

test-findings345:
	docker compose -f stemengine.yml --profile tests run --rm stemengine-tests

test-findings34: test-findings345

test-hardening: test-findings345

test-finding5:
	docker compose -f stemengine.yml --profile tests run --rm stemengine-tests bash -lc "python3.11 -m unittest backend.tests.test_ensemble_separator -v"

# Alembic helpers — run against the container's bundled DB path
alembic-upgrade:
	docker run --rm $(IMAGE) bash -lc \
	  "cd /app && python3.11 -m alembic -c $(BACKEND)/alembic.ini upgrade head"

alembic-heads:
	docker run --rm $(IMAGE) bash -lc \
	  "cd /app && python3.11 -m alembic -c $(BACKEND)/alembic.ini heads"
