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

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

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)

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

# Student implementation; teacher acceptance targets remain unchanged.
PUBLIC_PORT ?= 8447
BASE_URL = https://localhost:$(PUBLIC_PORT)
CA_FILE = $(CURDIR)/secrets/ca.pem
bootstrap:
	python3 scripts/bootstrap.py
build:
	VCS_REF=$$(git rev-parse HEAD) docker compose build
init: bootstrap build
	docker compose up -d postgres openbao cache cache-tls broker broker-tls storage s3 storage-tls
	python3 scripts/bao_operator.py init
up: init
	VCS_REF=$$(git rev-parse HEAD) docker compose up -d --wait
unseal:
	python3 scripts/bao_operator.py unseal
migrate:
	docker compose run --rm migrate
down:
	docker compose down
student-test:
	BASE_URL=$(BASE_URL) SSL_CERT_FILE=$(CA_FILE) uv run --locked pytest -q
lint:
	uv run --locked ruff check src student-tests scripts
.PHONY: bootstrap build init up unseal migrate down student-test lint
