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: up down test-idempotency test-cache test-uploads test-private test-attachment-failover test-dependencies research-memory research-consistency reconcile reconcile-apply
up:
	./scripts/bootstrap.sh
	./scripts/compose.sh up -d --build

# Preserve volumes; data deletion is an explicit operator action.
down:
	./scripts/compose.sh down

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

test-cache:
	./scripts/compose.sh exec -T -e BASE_URL=http://lb:8080 -e CACHE_URL=redis://valkey:6379/0 api python - < tests/cache_consistency.py

test-uploads:
	./scripts/compose.sh run --rm -T --no-deps ops python tests/uploads_negative.py

test-private:
	./scripts/compose.sh run --rm -T --no-deps ops python tests/private_storage.py

test-attachment-failover:
	$(PYTHON) tests/attachment_failover.py --base-url "$(BASE_URL)"

test-dependencies:
	$(PYTHON) scripts/test_dependency_outages.py

research-memory:
	$(PYTHON) scripts/research_upload_memory.py

research-consistency:
	$(PYTHON) scripts/research_consistency_host.py

reconcile:
	./scripts/compose.sh run --rm -T --no-deps ops python reconcile.py

reconcile-apply:
	./scripts/compose.sh run --rm -T --no-deps ops python reconcile.py --apply

.PHONY: plot-research
plot-research:
	$(PYTHON) scripts/plot_research.py
	$(PYTHON) scripts/summarize_research.py
