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 test-attachments check-private-bucket upload-mem reconcile-track2

help:
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
	@echo "test-attachments: student negative cases; check-private-bucket: MinIO ACL; upload-mem: 10 concurrent uploads"
	@echo "reconcile-track2: inject both drifts, then time the walk at 10/100/400 extra objects"

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-attachments:
	$(PYTHON) tests/attachments_negative.py --base-url "$(BASE_URL)" $(TLS_ARGS)

check-private-bucket:
	$(PYTHON) scripts/check_private_bucket.py

upload-mem:
	$(PYTHON) scripts/upload_mem.py --base-url "$(BASE_URL)" --output evidence/upload-concurrency.json

reconcile-track2:
	docker compose --env-file .env --profile ops build reconcile
	docker compose --env-file .env --profile ops run --rm -T reconcile --experiment > evidence/reconcile-track2.json
	docker compose --env-file .env --profile ops run --rm -T reconcile --bench 10,100,400 > evidence/reconcile-walk.json
