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 load test-persistence test-failover

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)

.PHONY: test-attachments test-private test-upload-failover test-s3-outage reconcile reconcile-apply test-reconcile test-reconcile-benchmark test-upload-memory test-s3-permissions
test-attachments:
	uv run python student-tests/attachments.py --base-url "$(BASE_URL)"

test-private:
	$(PYTHON) student-tests/attachment_resilience.py --mode private --base-url "$(BASE_URL)"

test-upload-failover:
	$(PYTHON) student-tests/attachment_resilience.py --mode failover --base-url "$(BASE_URL)"

test-s3-outage:
	$(PYTHON) student-tests/attachment_resilience.py --mode s3-outage --base-url "$(BASE_URL)"

reconcile:
	docker compose --profile maintenance run --rm --no-deps reconcile --grace-seconds 3600

reconcile-apply:
	docker compose --profile maintenance run --rm --no-deps reconcile --apply --grace-seconds 3600

test-reconcile:
	$(PYTHON) student-tests/reconcile.py --base-url "$(BASE_URL)"

test-reconcile-benchmark:
	$(PYTHON) student-tests/reconcile_benchmark.py --base-url "$(BASE_URL)"

.PHONY: test-reconcile-audit audit
test-reconcile-audit:
	$(PYTHON) student-tests/reconcile_audit.py --base-url "$(BASE_URL)"

audit:
	$(PYTHON) scripts/audit.py

test-upload-memory:
	uv run python student-tests/upload_memory.py --base-url "$(BASE_URL)"

test-s3-permissions:
	$(PYTHON) student-tests/s3_permissions.py --base-url "$(BASE_URL)"
