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

ENV_FILE ?= .env
BASE_URL ?= http://localhost:8080
CA_FILE ?=
ACTION_SCRIPT ?=

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-level4-transactions \
	test-session-security \
	test-cache-fallback \
	test-database-failure \
	explain-history \
	test-level4 \
	test-track2 \
	test-level5 \
	compose-config \
	compose-build \
	compose-up \
	compose-tools-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 a running API"
	@echo "load: run synthetic load against a running API"
	@echo "test-persistence: run persistence test with ACTION_SCRIPT"
	@echo "test-level4-transactions: verify username race and chat rollback"
	@echo "test-session-security: verify short TTL, wrong password and logout"
	@echo "test-cache-fallback: verify API fallback while Redis is stopped"
	@echo "test-database-failure: verify bounded errors while PostgreSQL is stopped"
	@echo "explain-history: run EXPLAIN on a rolled-back 10000-message fixture"
	@echo "test-level4: run the complete cumulative level 4 verification"
	@echo "test-track2: benchmark 100000 messages and verify backup/restore"
	@echo "test-level5: run cumulative level 4 checks and Track 2"
	@echo "compose-config: validate resolved Compose configuration"
	@echo "compose-build: build application images"
	@echo "compose-up: start the stack"
	@echo "compose-tools-up: start optional Redis Insight"
	@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-level4-transactions:
	$(PYTHON) tests/level4_transactions.py \
		--base-url "$(BASE_URL)" \
		$(TLS_ARGS)

test-session-security:
	$(PYTHON) tests/session_security.py \
		--action-script "scripts/session_ttl.sh" \
		--ttl-seconds 2 \
		--base-url "$(BASE_URL)" \
		$(TLS_ARGS)

test-cache-fallback:
	$(PYTHON) tests/cache_resilience.py \
		--action-script "scripts/redis_outage.sh" \
		--base-url "$(BASE_URL)" \
		$(TLS_ARGS)

test-database-failure:
	$(PYTHON) tests/database_resilience.py \
		--action-script "scripts/postgres_outage.sh" \
		--base-url "$(BASE_URL)" \
		$(TLS_ARGS)

explain-history:
	scripts/explain_history.sh

test-level4:
	$(MAKE) go-check
	$(MAKE) check
	$(MAKE) validate
	$(MAKE) compose-config
	$(MAKE) test
	$(MAKE) test-level4-transactions
	$(MAKE) test-session-security
	$(MAKE) test-cache-fallback
	$(MAKE) test-database-failure
	$(MAKE) test-persistence ACTION_SCRIPT=scripts/restart.sh
	$(MAKE) explain-history

test-track2:
	scripts/run_track2.sh

test-level5:
	$(MAKE) test-level4
	$(MAKE) test-track2

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-tools-up:
	$(COMPOSE_CMD) --profile tools up -d redis-insight

compose-down:
	$(COMPOSE_CMD) down

compose-ps:
	$(COMPOSE_CMD) ps

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