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

.PHONY: help check validate test test-idempotency test-readiness-gate load test-persistence test-failover test-failover-each test-rolling swarm-up test-node-failover swarm-down

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)

test-idempotency:
	$(PYTHON) student_tests/idempotency_concurrent.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-readiness-gate:
	$(PYTHON) student_tests/readiness_gate.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 "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-failover:
	$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-failover-each:
	$(PYTHON) student_tests/failover_each.py --action-script "$(ACTION_SCRIPT)" --restore-script "$(RESTORE_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-rolling:
	$(PYTHON) student_tests/rolling_update.py --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

swarm-up:
	powershell -NoProfile -ExecutionPolicy Bypass -File scripts/swarm-lab-up.ps1

test-node-failover:
	$(PYTHON) student_tests/node_failover.py --base-url "http://localhost:18080" --output "evidence/node-failover.json"

swarm-down:
	powershell -NoProfile -ExecutionPolicy Bypass -File scripts/swarm-lab-down.ps1
