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

.PHONY: help check validate up down test load test-persistence test-failover test-security test-tls test-acl test-compromise incident scan

help:
	@echo "up: bootstrap PKI/secrets + OpenBao + stack; down: stop (volumes kept); test/load/test-persistence/test-failover/test-security: public checks"
	@echo "test-tls: whole trust matrix; test-acl: ACL/rate-limit/leaks; test-compromise: track 1 probes; incident: track 1 recovery; scan: dependency+image scan"

check:
	$(PYTHON) scripts/check_contract.py

validate:
	$(PYTHON) scripts/validate_openapi.py

up:
	sh scripts/up.sh

down:
	docker compose stop

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

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

test-persistence:
	$(PYTHON) tests/resilience.py --mode persistence --action-script "$(or $(ACTION_SCRIPT),scripts/restart.sh)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-failover:
	$(PYTHON) tests/resilience.py --mode failover --action-script "$(or $(ACTION_SCRIPT),scripts/stop-one.sh)" --base-url "$(BASE_URL)" $(TLS_ARGS)

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

test-tls:
	sh security-tests/run_in_net.sh tls_audit.py

test-acl:
	$(PYTHON) security-tests/acl_leak.py $(TLS_ARGS) --grafana-password-file secrets/ops/grafana_admin_password

test-compromise:
	sh security-tests/run_in_net.sh worker_compromise.py --profile after > evidence/worker-compromise-after.json
	sh security-tests/run_in_net.sh worker_compromise.py --profile before > evidence/worker-compromise-before.json

incident:
	docker compose stop worker
	$(PYTHON) security-tests/jobs_integrity.py submit --n 10 $(TLS_ARGS)
	sh scripts/incident_worker.sh
	cd "$$PWD" && $(PYTHON) security-tests/jobs_integrity.py verify $(TLS_ARGS)

scan:
	sh scripts/scan.sh
