.PHONY: help test test-race test-e2e cover db-up db-reset run build lint

help:
	@echo "test       run unit and integration tests"
	@echo "test-race  run tests under the race detector"
	@echo "test-e2e   run end-to-end tests against a live server"
	@echo "cover      report coverage per package"
	@echo "db-up      start postgres and redis"
	@echo "db-reset   destroy and recreate the database"
	@echo "run        run the server with the dev faucet enabled"
	@echo "build      build the server binary"

test: db-up
	go test ./... -count=1

test-race: db-up
	go test ./pkg/... -race -count=1

# The end-to-end suite drives a live server; start one first.
test-e2e: db-up build
	@echo "start the server with 'make run' in another shell, then:"
	@echo "  ARCADE_E2E=http://localhost:8080 go test ./cmd/arcade/ -v"

cover: db-up
	go test ./pkg/... -count=1 -coverprofile=coverage.out
	go tool cover -func=coverage.out | tail -1

db-up:
	@docker compose up -d postgres redis >/dev/null
	@for i in $$(seq 1 40); do \
		docker compose exec -T postgres pg_isready -U arcade >/dev/null 2>&1 && exit 0; \
		sleep 1; \
	done; echo "postgres did not become ready" >&2; exit 1

# The ledger is append-only, so tests that mint large amounts steadily consume
# the bridge's headroom. This wipes the volume and reapplies every migration.
db-reset:
	docker compose down -v
	docker compose up -d postgres
	@for i in $$(seq 1 40); do \
		docker compose exec -T postgres pg_isready -U arcade >/dev/null 2>&1 && break; \
		sleep 1; \
	done
	@# Migrations are mounted into docker-entrypoint-initdb.d and run
	@# automatically on a fresh volume, so there is nothing to apply here.
	@docker compose exec -T postgres psql -q -U arcade -d arcade \
		-c "SELECT count(*) AS tables FROM information_schema.tables WHERE table_schema='public'"
	@echo "database reset"

run: db-up
	ARCADE_DEV_FAUCET=1 go run ./cmd/arcade

build:
	go build -o bin/qa ./cmd/arcade

lint:
	go vet ./...
	gofmt -l .
