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)

# ---------------------------------------------------------------------------
# Application targets (Go implementation). The targets above are the skeleton.
# ---------------------------------------------------------------------------
EVIDENCE_DIR ?= evidence

.PHONY: build vet unit secrets up down destroy logs concurrency explain instances rollout bench plot failover-load rollout-load

build:
	go build -o bin/server ./cmd/server

vet:
	go vet ./...

# The tests start disposable PostgreSQL and Valkey containers: Docker must run.
unit:
	go test -race -count=1 ./...

secrets:
	sh scripts/init-secrets.sh

up:
	docker compose up -d --build --wait
	sh scripts/wait-ready.sh

# Stops and removes the containers; volumes and therefore data are kept.
down:
	docker compose down

# Deletes the volumes too: every user, chat, message and session of the stand.
destroy:
	docker compose down -v

logs:
	docker compose logs

# Safe default: inspection only; APPLY=1 explicitly enables repairs.
APPLY ?= 0
DEEP ?= 0
.PHONY: reconcile upload-memory reconcile-bench
reconcile:
	docker compose --profile recon run --rm reconcile $(if $(filter 1,$(APPLY)),--apply,) $(if $(filter 1,$(DEEP)),--deep,)

upload-memory:
	$(PYTHON) -X utf8 scripts/lab4/upload-memory.py --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/upload-memory.json"

reconcile-bench:
	go run ./tools/reconcilebench --output "$(EVIDENCE_DIR)/track2/results.json"

# Which container answers under which X-Instance-Id.
instances:
	sh scripts/instances.sh

# Replaces every API replica without downtime; all of them must be running.
rollout:
	sh scripts/rollout.sh

# Track 1. Every run starts from empty volumes, so the stand's data is lost.
# make bench SERIES="scripts/bench/series/A.conf scripts/bench/series/B.conf" RATES=400,800,1200
SERIES ?= scripts/bench/series/A.conf
bench:
	sh scripts/bench/run-series.sh $(SERIES)

# Needs matplotlib from requirements-bench.txt: make plot PYTHON=<venv python>.
plot:
	$(PYTHON) scripts/bench/plot.py evidence/track1/raw evidence/track1

# Level 4 under load: one replica stopped (KIND=stop) or killed (KIND=kill),
# and the rolling update.
KIND ?= stop
REPLICA ?= 1
failover-load:
	sh scripts/bench/failover.sh $(KIND) $(REPLICA)

rollout-load:
	sh scripts/bench/rollout-under-load.sh

concurrency:
	go run ./tools/concurrency --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/concurrency.json"

explain:
	docker compose exec -T postgres psql -U messenger -d messenger -X -q -v ON_ERROR_STOP=1 -f - < scripts/explain-history.sql
