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 solution (lab 7, Python). On Windows: Git Bash `sh` in PATH (C:\Program Files\Git\usr\bin),
# PYTHONUTF8=1, PYTHON=.venv/Scripts/python; the hooks are started through scripts/*.cmd.
# Full first start from a clean clone (docs/OPERATOR.md):  make secrets && make up
CA ?= secrets/pki/public-ca.crt
.PHONY: secrets up bao-bootstrap bao-unseal bao-status down logs ps migrate lb-status restore rolling-update worker-stop worker-start jobs failed audit student-test unit-test security-test mtls-check bao-check argon2-bench explain grafana alerts trace-demo dashboard dashboard-check obs-test

# Local synthetic credentials + PKI (two CAs, service certificates) into ./secrets (git-ignored).
secrets:
	$(PYTHON) scripts/init_secrets.py

# OpenBao first (it starts sealed), operator init/unseal/configuration, then the whole stand.
up:
	docker compose up -d openbao
	$(PYTHON) scripts/bao_operator.py bootstrap
	docker compose up -d --build --wait

bao-bootstrap:
	$(PYTHON) scripts/bao_operator.py bootstrap

# After every restart of OpenBao (it comes back sealed).
bao-unseal:
	$(PYTHON) scripts/bao_operator.py unseal

bao-status:
	$(PYTHON) scripts/bao_operator.py status

# Safe stop: containers stop, named volumes (data, queue, secrets storage) stay.
down:
	docker compose down

logs:
	docker compose logs --tail 50 api lb relay worker openbao

ps:
	docker compose ps -a

migrate:
	docker compose run --rm migrate

# HAProxy view of the replicas (UP/DOWN) mapped to containers and X-Instance-Id aliases.
lb-status:
	$(PYTHON) scripts/lbctl.py status
	$(PYTHON) scripts/lbctl.py replicas

# Start replicas stopped by scripts/stop-one.sh again and wait until the LB has them all UP.
restore:
	sh scripts/restore-api.sh
	$(PYTHON) scripts/lbctl.py wait-up $${API_REPLICAS:-2}

# Replace every API replica one by one with a freshly built image (no downtime).
rolling-update:
	sh scripts/rolling-update.sh

worker-stop:
	docker compose stop worker

worker-start:
	docker compose start worker

jobs:
	docker compose run --rm --no-deps jobctl stats

failed:
	docker compose run --rm --no-deps jobctl list-failed

audit:
	docker compose run --rm --no-deps auditor

# Own tests: unit + black-box against the running stand (needs requirements*.txt in a venv).
student-test:
	$(PYTHON) -m pytest student-tests -v

unit-test:
	$(PYTHON) -m pytest student-tests -m "not stack" -q

# Lab 7: the trust matrix proofs only (TLS, mTLS, roles/ACL, OpenBao, rate limit, hardening).
security-test:
	$(PYTHON) -m pytest student-tests/test_security.py -v

# Lab 7 defense step 3: LB -> API mTLS (no cert / foreign CA / wrong identity / lb).
mtls-check:
	sh scripts/mtls_check.sh

# Lab 7 defense step 4: OpenBao allow/deny for api/relay/worker + no root credential in the apps.
bao-check:
	sh scripts/bao_check.sh

# Lab 7: Argon2id parameter measurement inside an API replica (its CPU/RAM limits).
argon2-bench:
	docker compose exec -T api python - < bench/argon2_bench.py > evidence/argon2/bench.json

explain:
	$(PYTHON) scripts/explain_history.py --output evidence/explain-history.txt

# Lab 6: observability (docs/OBSERVABILITY.md, docs/SLO.md, docs/RUNBOOK.md)
grafana:
	@echo "Grafana: https://localhost:3000 (CA $(CA)), user admin, password in secrets/grafana_admin_password"

alerts:
	$(PYTHON) scripts/obs.py alerts

# Controlled request with our own traceparent -> its trace in Tempo + its logs in Loki.
trace-demo:
	$(PYTHON) scripts/trace_demo.py --output evidence/trace-demo.txt

dashboard:
	$(PYTHON) scripts/build_dashboard.py

dashboard-check:
	$(PYTHON) scripts/check_dashboard.py --output evidence/dashboard-check.txt

obs-test:
	$(PYTHON) -m pytest student-tests/test_observability.py -v
