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)

# ---- student targets (lab4 solution) ----
.PHONY: secrets up down ps restore test-student ops-checks memory track2-faults track2-cost

secrets:
	$(PYTHON) scripts/init_secrets.py

up: secrets
	docker compose up -d --build --wait

down:
	docker compose stop

ps:
	docker compose ps -a

restore:
	docker compose up -d --wait

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

ops-checks:
	$(PYTHON) scripts/lab4ops.py private --output evidence/private.json
	$(PYTHON) scripts/lab4ops.py failover-upload --output evidence/failover-upload.json
	$(PYTHON) scripts/lab4ops.py no-local-file --output evidence/no-local-file.json
	$(PYTHON) scripts/lab4ops.py bootstrap --output evidence/bootstrap.json
	$(PYTHON) scripts/lab4ops.py s3-outage --output evidence/s3-outage.json

memory:
	$(PYTHON) scripts/lab4ops.py memory --output evidence/memory.json

track2-faults:
	$(PYTHON) scripts/track2/run_track2.py faults --output evidence/track2/faults.json

track2-cost:
	$(PYTHON) scripts/track2/run_track2.py cost --output evidence/track2/cost.json
