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)",)

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

help:
	@echo "app-up: build and start API, database and frontend (http://localhost:5173)"
	@echo "app-down: stop the application and keep PostgreSQL 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 tests with PostgreSQL and race detection in isolated schemas"
	@echo "test-persistence ACTION_SCRIPT=scripts/restart.sh: restart API and DB, verify saved 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)
