PYTHON ?= python3
BASE_URL ?= http://localhost:8080
CA_FILE ?=
ACTION_SCRIPT ?=
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)

.PHONY: help check validate test load test-persistence test-failover

help:
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"

check:
	$(PYTHON) scripts/check_contract.py

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)

# ---------------------------------------------------------------------------------------------
# Цели решения (lab3). Публичные цели выше не изменены.
# ---------------------------------------------------------------------------------------------
COMPOSE ?= docker compose --env-file .env
TRACK1 = COMPOSE_FILE=compose.yaml:research/compose.track1.yaml

.PHONY: secrets up down ps logs destroy-data restore rolling-update test-student test-api test-idempotency \
        test-expiry test-cache-race test-failures lb-experiments track1 track1-report evidence

secrets:
	./scripts/init-secrets.sh

# Единая команда запуска: LB + 2 реплики API + migrate (one-shot) + PostgreSQL + Redis (+ api-ttl для теста TTL).
up: secrets
	$(COMPOSE) --profile ttl up -d --build --wait

ps:
	$(COMPOSE) --profile ttl ps -a

logs:
	$(COMPOSE) logs --tail=100 lb api

# Безопасная остановка: контейнеры удаляются, volume pgdata сохраняется.
down:
	$(COMPOSE) --profile ttl --profile bench down

# НАМЕРЕННОЕ удаление учебных данных (volume pgdata). Необратимо.
destroy-data:
	@echo "This deletes ALL lab3 data volumes. Type 'yes' to continue:"; read ans; [ "$$ans" = yes ]
	$(COMPOSE) --profile ttl --profile bench down -v

# Вернуть реплики после make test-failover ACTION_SCRIPT=scripts/stop-one.sh
restore:
	./scripts/start-replicas.sh

rolling-update:
	./scripts/rolling-update.sh

test-api:
	$(PYTHON) student-tests/test_api.py

test-idempotency:
	$(PYTHON) student-tests/test_idempotency.py

test-expiry:
	$(PYTHON) student-tests/test_expiry.py

test-cache-race:
	$(COMPOSE) run --rm --no-deps -T -v "$(CURDIR)/student-tests:/app/student-tests:ro" --entrypoint python api student-tests/test_cache_race.py

# Останавливает/запускает redis и postgres (volumes не трогает).
test-failures:
	$(PYTHON) student-tests/test_failures.py

test-student: test-api test-idempotency test-expiry test-cache-race test-failures

# Уровень 4: отказ каждой реплики (SIGTERM и SIGKILL) под нагрузкой + rolling update под нагрузкой.
lb-experiments:
	$(PYTHON) research/lb_experiments.py failover-series --mode both
	$(PYTHON) research/lb_experiments.py rolling-update

# Трек 5.1 (≈1 ч): серии нагрузки; затем сводка и графики в evidence/track1/.
track1:
	$(TRACK1) $(COMPOSE) --profile bench build loadgen
	$(TRACK1) $(PYTHON) research/track1.py run all
	$(PYTHON) research/track1.py report

track1-report:
	$(PYTHON) research/track1.py report

evidence:
	./scripts/collect-evidence.sh
