PYTHON ?= python3
BASE_URL ?= http://localhost:8080
CA_FILE ?=
ACTION_SCRIPT ?=
COMPOSE = docker compose --env-file .env
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
RUN_ID ?=
TRACK2 = $(PYTHON) src/experiments/track2/run.py

.PHONY: help app-up app-down app-logs check validate test test-backend load test-persistence

help:
	@echo "track2-run RUN_ID=<unique-name>: isolated query, write and backup/restore experiment"
	@echo "track2-status / track2-stop / track2-clean RUN_ID=<name>: inspect, stop or delete only experiment volumes; archives remain"
	@echo "app-up: build and start API, PostgreSQL, Redis and frontend (http://localhost:5173)"
	@echo "app-down: stop the application and keep PostgreSQL and Redis data"
	@echo "app-logs: follow service logs"
	@echo "check: offline contract structure"
	@echo "validate: full OpenAPI validation (install requirements-dev.txt first)"
	@echo "test: public API smoke test (run app-up first)"
	@echo "test-backend: Go race tests with PostgreSQL and Redis in isolated schemas"
	@echo "test-persistence ACTION_SCRIPT=scripts/restart.sh: restart API, PostgreSQL and Redis; verify data and session (run app-up first)"
	@echo "load: synthetic API writes"

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

app-down:
	$(COMPOSE) down

app-logs:
	$(COMPOSE) logs -f

check:
	$(PYTHON) scripts/check_contract.py

validate:
	$(PYTHON) scripts/validate_openapi.py

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

test-backend:
	$(COMPOSE) --profile test run --build --rm backend-tests

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)

.PHONY: track2-run track2-status track2-stop track2-clean
track2-run:
	$(TRACK2) run --run-id "$(RUN_ID)"

track2-status:
	$(TRACK2) status --run-id "$(RUN_ID)"

track2-stop:
	$(TRACK2) stop --run-id "$(RUN_ID)"

track2-clean:
	$(TRACK2) clean --run-id "$(RUN_ID)"
