PYTHON ?= python3
BASE_URL ?= https://localhost:8443
CA_FILE ?= $(CURDIR)/.local/trust/ca.crt
ACTION_SCRIPT ?=
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)

.PHONY: help bootstrap up down check validate lint format test test-security test-lab7 test-lifecycle test-audit test-attachments test-api-regressions test-persistence test-failover test-both-failovers verify-lab7 scan
help:
	@echo "bootstrap; check validate lint; test test-security; test-lab7; test-lifecycle; test-audit; test-both-failovers; scan"
bootstrap:
	$(PYTHON) scripts/lab7.py bootstrap
up: bootstrap
down:
	docker compose down
check:
	$(PYTHON) scripts/check_contract.py
validate:
	$(PYTHON) scripts/validate_openapi.py
lint:
	$(PYTHON) -m ruff check src
	$(PYTHON) -m ruff format --check src
format:
	$(PYTHON) -m ruff check --fix src
	$(PYTHON) -m ruff format src
test:
	$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-security:
	$(PYTHON) tests/security.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-lab7:
	$(PYTHON) tests/lab7_security.py
test-lifecycle:
	$(PYTHON) tests/lab7_lifecycle.py
test-audit:
	$(PYTHON) tests/lab7_audit.py
test-attachments:
	$(PYTHON) tests/attachments.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-api-regressions:
	$(PYTHON) tests/api_regressions.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-persistence:
	@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT=scripts/restart.sh"; 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=scripts/stop-one.sh"; exit 2)
	$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
test-both-failovers:
	$(PYTHON) scripts/test-failovers.py
verify-lab7:
	$(PYTHON) scripts/verify-lab7.py
scan:
	$(PYTHON) scripts/scan.py
