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

export BUILDX_NO_DEFAULT_ATTESTATIONS := 1

.PHONY: help check validate test load test-persistence test-failover install init up down restart restore instances rolling-update ps logs wait-ready destroy-data student-test concurrency-test test-expiry test-failures test-migrations explain sigterm-check experiment-failover experiment-rolling test-storage test-s3-failure janitor-dry-run janitor-once experiment-uploads s3-ui

help:
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
	@echo "up/down/restart/ps/logs: compose stack; destroy-data: DELETE volumes intentionally"
	@echo "instances: API containers <-> X-Instance-Id; restore: start replicas stopped by stop-one.sh; rolling-update: replace replicas one by one"
	@echo "experiment-failover / experiment-rolling: load + stop each replica / rolling update, metrics to evidence/"
	@echo "student-test: own tests; test-migrations: from empty volume; test-expiry: short SESSION_TTL; test-failures: stop Valkey/PostgreSQL/S3; explain: EXPLAIN history"
	@echo "init: .env + local S3 secrets; test-storage: private bucket/credentials/consistency; janitor-dry-run / janitor-once: S3/DB reconcile"
	@echo "experiment-uploads: >=10 concurrent uploads, peak API memory to evidence/"
	@echo "s3-ui: temporary read-only SeaweedFS Filer UI on 127.0.0.1:8888 (Ctrl+C to close)"

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)

install:
	poetry install --no-root

init:
	./scripts/init-secrets.sh

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

down:
	docker compose --profile expiry down

restart:
	./scripts/restart.sh
	@$(MAKE) --no-print-directory wait-ready

restore:
	./scripts/restore.sh

instances:
	./scripts/instances.sh

rolling-update:
	./scripts/rolling-update.sh

ps:
	docker compose ps

logs:
	docker compose logs -f api nginx

wait-ready:
	@for i in $$(seq 1 60); do \
		curl -fsS "$(BASE_URL)/health/ready" >/dev/null 2>&1 && echo "ready: $(BASE_URL)" && exit 0; \
		sleep 1; \
	done; echo "API is not ready after 60s"; exit 1

destroy-data:
	docker compose --profile expiry down -v

student-test:
	BASE_URL="$(BASE_URL)" poetry run pytest

concurrency-test:
	BASE_URL="$(BASE_URL)" poetry run pytest student-tests/test_concurrency.py -s

test-expiry:
	docker compose --profile expiry up -d --build --wait api-short-ttl
	EXPIRY_BASE_URL=http://localhost:8081 poetry run pytest student-tests/test_expiry.py -s; \
		status=$$?; docker compose --profile expiry stop api-short-ttl; exit $$status

test-failures:
	BASE_URL="$(BASE_URL)" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_dependency_failures.py student-tests/test_storage_failures.py student-tests/test_attachment_failover.py -s

test-s3-failure:
	BASE_URL="$(BASE_URL)" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_storage_failures.py -s

test-storage:
	BASE_URL="$(BASE_URL)" RUN_STORAGE_TESTS=1 poetry run pytest student-tests/test_storage_security.py student-tests/test_storage_consistency.py -s

janitor-dry-run:
	docker compose exec -T janitor python src/janitor.py --once --dry-run

janitor-once:
	docker compose exec -T janitor python src/janitor.py --once

s3-ui:
	$(PYTHON) scripts/s3_ui.py

experiment-uploads:
	poetry run python scripts/upload_experiment.py --base-url "$(BASE_URL)" --output evidence/experiment-uploads.json

test-migrations:
	./scripts/check_migrations.sh

explain:
	docker compose exec -T postgres sh -c 'psql -X -q -v ON_ERROR_STOP=1 -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"' \
		< scripts/explain_history.sql > evidence/explain_history.txt
	@echo "saved: evidence/explain_history.txt"

sigterm-check:
	$(PYTHON) scripts/sigterm_check.py --base-url "$(BASE_URL)"

experiment-failover:
	$(PYTHON) scripts/experiment.py failover --base-url "$(BASE_URL)" --output evidence/experiment-failover.json

experiment-rolling:
	$(PYTHON) scripts/experiment.py rolling --base-url "$(BASE_URL)" --output evidence/experiment-rolling.json
