PYTHON ?= python3
ifndef BASE_URL
BASE_URL := $(shell $(PYTHON) scripts/runtime_config.py)
endif
export BASE_URL
CA_FILE ?=
API_CONTAINER = $(shell docker compose ps -q api | head -n 1)
ACTION_SCRIPT ?=
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)

.PHONY: help check validate test load test-persistence

help:
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; test-lab3: concurrency; verify-lab3: full acceptance; research-lab3: scaling study; 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)

.PHONY: test-persistence-proof
test-persistence-proof:
	$(PYTHON) scripts/test-persistence.py --action-script "scripts/restart.sh" --base-url "$(BASE_URL)" $(TLS_ARGS)

.PHONY: up down migrate test-extra
up:
	docker compose up -d --build --wait

down:
	docker compose down

migrate:
	docker compose run --rm migration

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

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

.PHONY: test-production test-production-limits audit-dependencies
test-production:
	$(PYTHON) tests/production.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-production-limits:
	docker exec -i "$(API_CONTAINER)" python < tests/production_limits.py

audit-dependencies:
	$(PYTHON) scripts/audit-dependencies.py $(TLS_ARGS)

.PHONY: test-db test-expiry test-dependencies
test-db:
	docker exec -i "$(API_CONTAINER)" python < tests/database.py

test-expiry:
	PYTHON="$(PYTHON)" scripts/test-expiry.sh

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

.PHONY: test-passwords
test-passwords:
	docker exec -i "$(API_CONTAINER)" python < tests/passwords.py

.PHONY: lint format
lint:
	$(PYTHON) -m ruff check src
	$(PYTHON) -m ruff format --check src

format:
	$(PYTHON) -m ruff check --fix src
	$(PYTHON) -m ruff format src

.PHONY: test-auth-logs
test-auth-logs:
	$(PYTHON) tests/auth_logging.py --base-url "$(BASE_URL)" $(TLS_ARGS)

.PHONY: test-settings
test-settings:
	docker exec -i "$(API_CONTAINER)" python < tests/settings.py

.PHONY: test-callbacks
test-callbacks:
	docker exec -i "$(API_CONTAINER)" python < tests/callbacks.py

.PHONY: test-failover test-lab3 rolling-update
test-failover:
	@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT=scripts/stop-one.sh"; exit 2)
	$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

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

rolling-update:
	$(PYTHON) scripts/rolling-update.py

.PHONY: verify-lab3 research-lab3
verify-lab3:
	$(PYTHON) scripts/verify-lab3.py

research-lab3:
	$(PYTHON) scripts/research-lab3.py --full
	$(PYTHON) scripts/plot-lab3.py

.PHONY: audit-lab3
audit-lab3:
	$(PYTHON) scripts/audit-lab3.py

.PHONY: audit-edges-lab3
audit-edges-lab3:
	$(PYTHON) scripts/edge-audit-lab3.py

.PHONY: review-lab3
review-lab3:
	$(PYTHON) scripts/review-lab3.py
