PYTHON ?= python3
BASE_URL ?= http://localhost:8080
CA_FILE ?=
ACTION_SCRIPT ?=
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
COMPOSE_ENV_FILE ?= .env
COMPOSE = docker compose --env-file $(COMPOSE_ENV_FILE)
LAB3 = $(PYTHON) scripts/lab3/lab3.py
FAILOVER_RUNS ?= 3
RATE ?= 200
TRACK1_ARGS ?=

.PHONY: help check test-analytics validate test load test-persistence test-failover test-dependencies lab3-before-image \
	stand-up stand-down stand-status stand-replica wait-ready quiesce-up quiesce-down \
	test-e2e test-failover-all rolling-update track1 evidence

help:
	@echo "check: offline structure; test-analytics: offline analytics regression (no stand); validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes; test-dependencies: stops/restarts Redis and PostgreSQL"
	@echo "stand-up/stand-down/stand-status: HAProxy + API x API_REPLICAS over shared PostgreSQL and Redis"
	@echo "stand-replica: restore one missing API replica; wait-ready: wait for /health/ready"
	@echo "quiesce-up/quiesce-down: stop and restore unrelated background containers of the host"
	@echo "test-e2e: app black-box suite against the stand; test-failover-all: failover for every replica in turn"
	@echo "rolling-update: replace replicas one by one under load; track1: full scaling-price series; evidence: rebuild aggregates and graphs"

check:
	$(PYTHON) scripts/check_contract.py

# Регрессия аналитики трека 1: успешный RPS отделён от частоты ответов, рост
# сравнивается только внутри одного темпа. Оффлайн: сырые сводки — фикстуры.
test-analytics:
	$(PYTHON) -m unittest discover -s tests -p 'test_*.py' -v

validate:
	$(PYTHON) scripts/validate_openapi.py

test:
	$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)

load:
	$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-persistence:
	@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
	$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-failover:
	@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
	$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-dependencies:
	cd app && E2E_COMPOSE_DIR="$(CURDIR)" E2E_COMPOSE_ENV_FILE="$(COMPOSE_ENV_FILE)" npm run test:dependencies

stand-up:
	$(COMPOSE) up -d --build --wait

stand-down:
	$(COMPOSE) down

stand-status:
	$(COMPOSE) ps

stand-replica:
	scripts/start-replica.sh

wait-ready:
	@for attempt in $$(seq 1 60); do \
		if curl -fsS "$(BASE_URL)/health/ready" >/dev/null 2>&1; then \
			echo "stand is ready ($(BASE_URL))"; exit 0; \
		fi; \
		sleep 1; \
	done; \
	echo "stand did not become ready within 60s" >&2; exit 1

quiesce-up:
	scripts/quiesce.sh up

quiesce-down:
	scripts/quiesce.sh down

test-e2e:
	scripts/app-e2e.sh

test-failover-all:
	@for run in $$(seq 1 $(FAILOVER_RUNS)); do \
		echo "=== failover run $$run of $(FAILOVER_RUNS)"; \
		$(MAKE) test-failover ACTION_SCRIPT=scripts/stop-one.sh || exit 1; \
		scripts/start-replica.sh; \
		$(MAKE) wait-ready; \
	done

rolling-update:
	$(LAB3) rolling-update --under-load --rate $(RATE)

track1:
	$(LAB3) track1 $(TRACK1_ARGS)

# Образ «до» для A/B: дерево коммита C1 (путь записи из 8 обращений) с тем же
# Dockerfile. После сборки: $(LAB3) ab --before mtusi-messenger:lab3-before --after <образ C2>
C1_COMMIT ?= e9cd28d
lab3-before-image:
	rm -rf /tmp/lab3-before && mkdir -p /tmp/lab3-before
	git archive $(C1_COMMIT) | tar -x -C /tmp/lab3-before
	docker build --build-arg BUILD_ID=before -t mtusi-messenger:lab3-before /tmp/lab3-before

evidence:
	$(LAB3) analyze
