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

.PHONY: help check validate test load test-grade4 test-grade4-pipeline test-outbox-recovery test-worker-redelivery test-replay-failed-job test-broker-persistence test-persistence test-failover

help:
	@echo "check: offline structure; validate: full OpenAPI; test: public smoke; test-grade4: image pipeline, broker persistence, outbox recovery, redelivery and safe replay"

check:
	$(PYTHON) scripts/check_contract.py

validate:
	$(PYTHON) scripts/validate_openapi.py

test:
	$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-grade4: test test-grade4-pipeline test-broker-persistence test-outbox-recovery test-worker-redelivery test-replay-failed-job

test-grade4-pipeline:
	$(PYTHON) student-tests/pipeline_unit.py
	$(PYTHON) student-tests/grade4_pipeline.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-outbox-recovery:
	$(PYTHON) student-tests/outbox_recovery.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-worker-redelivery:
	$(PYTHON) student-tests/worker_redelivery.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-replay-failed-job:
	$(PYTHON) student-tests/replay_failed_job.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-broker-persistence:
	$(PYTHON) student-tests/broker_persistence.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)
