-include .env
API_PORT ?= 18080
PYTHON ?= $(if $(wildcard .venv/bin/python),.venv/bin/python,python3)
BASE_URL = http://127.0.0.1:$(API_PORT)
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"
	@echo "setup: Python dependencies; up/down: start/stop mtusi2026lab3; build: C# build"
	@echo "student-test: concurrent idempotency; test-auth/test-ui: authentication and web history"
	@echo "test-persistence/test-failover: restart/stop-one; restore-api: bring back replicas"
	@echo "test-grade4: each replica failure and sequential replacement under traffic"
	@echo "research-grade5: 1/2/4 replicas and resource limits; verify-grade5: full sequence"

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: ACTION_SCRIPT = scripts/restart.sh
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)
	$(PYTHON) student-tests/persistence_checks.py --base-url "$(BASE_URL)"

test-failover: ACTION_SCRIPT = scripts/stop-one.sh
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)

.PHONY: plots up down build setup student-test test-ui test-auth test-expiry test-idempotency-modes test-grade4 research-grade5 verify-grade5 rolling-update restore-api

up:
	./scripts/init-secrets.sh
	./scripts/compose.sh build api
	./scripts/compose.sh up -d --wait --wait-timeout 90

down:
	./scripts/compose.sh down

build:
	dotnet build src/Messenger.Api/Messenger.Api.csproj --nologo

setup:
	python3 -m venv .venv
	.venv/bin/python -m pip install -r requirements-dev.txt
	.venv/bin/python -m pip install -r requirements-research.txt

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

test-ui:
	$(PYTHON) student-tests/ui_api_tests.py --base-url "$(BASE_URL)"

.PHONY: test-showcase
test-showcase:
	$(PYTHON) student-tests/showcase_tests.py --base-url "$(BASE_URL)"

restore-api:
	./scripts/compose.sh up -d --wait --wait-timeout 60 api lb

rolling-update:
	$(PYTHON) scripts/rolling_update.py --base-url "$(BASE_URL)"

test-grade4:
	$(PYTHON) student-tests/availability_tests.py --base-url "$(BASE_URL)"

test-auth:
	$(PYTHON) student-tests/lab2_tests.py --base-url "$(BASE_URL)"

test-expiry:
	$(PYTHON) student-tests/mode_tests.py --base-url "$(BASE_URL)" --expiry

test-idempotency-modes:
	$(PYTHON) student-tests/mode_tests.py --base-url "$(BASE_URL)"

research-grade5:
	$(PYTHON) student-tests/scaling_research.py --base-url "$(BASE_URL)"

plots:
	$(PYTHON) scripts/plot_results.py

verify-grade5:
	BASE_URL="$(BASE_URL)" PYTHON="$(PYTHON)" ./scripts/verify.sh
