# Code generation for the shared wire contracts (proto push channel + the
# FlatBuffers edge payloads). The generated Go is COMMITTED; CI only builds it,
# so this Makefile is a dev-time tool (the same model as backend/cmd/jetgen).
#
# Prerequisites:
#   make tools                 # go install the local protoc-gen-* plugins
#   flatc $(REQUIRED_FLATC)    # for `make fbs`
# Then:
#   make gen                   # proto + fbs
.PHONY: gen proto fbs flatc-check tools

# Pinned flatc version: the committed Go bindings under fbs/scrabblefb/ and the
# flatbuffers Go runtime (go.mod) are on this version. Generating with a
# different flatc silently churns output and can flip wire defaults.
REQUIRED_FLATC := 23.5.26

GOBIN := $(shell go env GOBIN)
ifeq ($(GOBIN),)
GOBIN := $(shell go env GOPATH)/bin
endif

gen: proto fbs

# tools installs the local buf plugins. Pin versions to the runtime libraries in
# go.mod (google.golang.org/protobuf, google.golang.org/grpc).
tools:
	go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.36.11
	go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.5.1

proto:
	PATH="$(GOBIN):$$PATH" buf generate

flatc-check:
	@command -v flatc >/dev/null || { echo "flatc not found; install flatc $(REQUIRED_FLATC)"; exit 1; }
	@flatc --version | grep -q "$(REQUIRED_FLATC)" || { echo "flatc $(REQUIRED_FLATC) required; found '$$(flatc --version)'"; exit 1; }

# --go-module-name rewrites cross-table imports to the fully-qualified module
# path so the generated code links without local replace directives.
fbs: flatc-check
	flatc --go --go-module-name scrabble/pkg/fbs -o fbs fbs/scrabble.fbs
