GO ?= go
PYTHON ?= python3
COMPOSE ?= docker compose

ENV_FILE ?= .env
BASE_URL ?= http://localhost:8080
CA_FILE ?=
ACTION_SCRIPT ?=
GRADE4_OUTPUT ?= evidence/grade4/results.json
GRADE5_OUTPUT ?= evidence/grade5/results.json
READINESS_OUTPUT ?= evidence/readiness-routing.json

TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
COMPOSE_CMD = $(COMPOSE) --env-file "$(ENV_FILE)"

.PHONY: \
	help \
	init-env \
	fmt \
	fmt-check \
	build \
	go-test \
	go-race \
	go-vet \
	go-check \
	check \
	validate \
	test \
	load \
	test-persistence \
	test-failover \
	test-readiness-routing \
	rolling-update \
	grade4-experiment \
	grade5-experiment \
	compose-config \
	compose-build \
	compose-up \
	compose-down \
	compose-ps \
	compose-logs

help:
	@echo "init-env: create local .env from .env.example if missing"
	@echo "fmt: format Go source files"
	@echo "fmt-check: report unformatted Go source files"
	@echo "build: compile the API"
	@echo "go-test: run Go tests"
	@echo "go-race: run Go tests with race detector"
	@echo "go-vet: run static Go analysis"
	@echo "go-check: run formatting, tests, race detector and vet"
	@echo "check: run offline skeleton checks"
	@echo "validate: validate the OpenAPI contract"
	@echo "test: run public smoke test against the LB entrypoint"
	@echo "load: run synthetic writes against the LB entrypoint"
	@echo "test-persistence: run persistence test with ACTION_SCRIPT"
	@echo "test-failover: run replica failover test with ACTION_SCRIPT"
	@echo "test-readiness-routing: isolate PostgreSQL from one API and verify active LB health checks"
	@echo "rolling-update: replace API replicas one by one with surge capacity"
	@echo "grade4-experiment: run failovers, idempotency and rolling update with evidence"
	@echo "grade5-experiment: measure fixed-budget scaling, saturation and one improvement"
	@echo "compose-config: validate resolved Compose configuration"
	@echo "compose-build: build application images"
	@echo "compose-up: start the stack"
	@echo "compose-down: stop the stack without deleting volumes"
	@echo "compose-ps: show stack containers"
	@echo "compose-logs: follow stack logs"

init-env:
	@if [ -f "$(ENV_FILE)" ]; then \
		echo "$(ENV_FILE) already exists; leaving it unchanged"; \
	else \
		cp .env.example "$(ENV_FILE)"; \
		echo "Created $(ENV_FILE) from .env.example"; \
	fi

fmt:
	$(GO) fmt ./...

fmt-check:
	@files="$$(find cmd internal student_tests -type f -name '*.go' -print)"; \
	unformatted="$$(gofmt -l $$files)"; \
	if [ -n "$$unformatted" ]; then \
		echo "Unformatted Go files:"; \
		echo "$$unformatted"; \
		exit 1; \
	fi

build:
	$(GO) build ./cmd/server

go-test:
	$(GO) test ./...

go-race:
	$(GO) test -race ./...

go-vet:
	$(GO) vet ./...

go-check: fmt-check go-test go-race go-vet

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)

test-readiness-routing:
	$(PYTHON) scripts/test_readiness_routing.py --env-file "$(ENV_FILE)" --output "$(READINESS_OUTPUT)"

rolling-update:
	ENV_FILE="$(ENV_FILE)" ./scripts/rolling-update.sh

grade4-experiment:
	$(PYTHON) scripts/grade4_experiment.py --base-url "$(BASE_URL)" --env-file "$(ENV_FILE)" --output "$(GRADE4_OUTPUT)"
	$(PYTHON) scripts/render_grade4.py "$(GRADE4_OUTPUT)"

grade5-experiment:
	$(PYTHON) scripts/grade5_experiment.py --base-url "$(BASE_URL)" --env-file "$(ENV_FILE)" --output "$(GRADE5_OUTPUT)"
	$(PYTHON) scripts/render_grade5.py "$(GRADE5_OUTPUT)"

compose-config:
	@test -f "$(ENV_FILE)" || \
		(echo "$(ENV_FILE) is missing; run: make init-env"; exit 2)
	$(COMPOSE_CMD) config --quiet

compose-build:
	$(COMPOSE_CMD) build

compose-up:
	$(COMPOSE_CMD) up -d --build

compose-down:
	$(COMPOSE_CMD) down

compose-ps:
	$(COMPOSE_CMD) ps

compose-logs:
	$(COMPOSE_CMD) logs --follow
