SDK_DIR := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
PROTO_DIR := $(SDK_DIR)/proto
PY_PACKAGE ?= loop_sdk
# The vendored contract is owned by the foundation module; its package root is
# loop/foundation/source. Generated Python lands directly under gen/vN.
PROTO_PACKAGE_ROOT ?= loop/foundation/source
GEN_DIR ?= $(SDK_DIR)/src/$(PY_PACKAGE)/gen
# Generation-only Buf config. buf.yaml keeps the module root at proto/ so
# lint/breaking enforce package-mirrored IDL paths. Python generation uses the
# contract owner directory as its root so generated modules land directly under
# gen/vN instead of gen/loop/foundation/source/vN.
GEN_BUF_CONFIG ?= {"version":"v2","modules":[{"path":"$(PROTO_PACKAGE_ROOT)"}]}
GEN_BUF_TEMPLATE ?= {"version":"v2","clean":true,"plugins":[{"remote":"buf.build/protocolbuffers/python","out":"$(GEN_DIR)"},{"remote":"buf.build/protocolbuffers/pyi","out":"$(GEN_DIR)"},{"remote":"buf.build/grpc/python","out":"$(GEN_DIR)"}]}

# Prerequisites:
#   - buf CLI on PATH (https://buf.build/docs/installation) — runs codegen via
#     remote plugins, lint, and breaking-change detection.
#   - uv (provides `uvx` to run protoletariat in an isolated env). protoletariat
#     rewrites absolute proto imports into relative ones; it is run isolated
#     because it caps protobuf <6 and must not touch the project runtime.
PROTOLETARIAT_SPEC ?= protoletariat

# Upstream contract location, for drift/breaking checks against the source of truth.
UPSTREAM_PROTO ?= ../loop/module/foundation/proto

.PHONY: proto proto-gen proto-lint proto-breaking proto-sync

## proto: lint then regenerate stubs (full pipeline)
proto: proto-lint proto-gen

## proto-gen: regenerate Python stubs for the vendored contract
proto-gen:
	cd "$(PROTO_DIR)" && buf generate --config '$(GEN_BUF_CONFIG)' --template '$(GEN_BUF_TEMPLATE)'
	cd "$(PROTO_DIR)" && buf build --config '$(GEN_BUF_CONFIG)' -o - | \
		uvx --from "$(PROTOLETARIAT_SPEC)" \
			protol --create-package --in-place --python-out "$(GEN_DIR)" raw -

## proto-lint: lint the vendored .proto contract
proto-lint:
	cd "$(PROTO_DIR)" && buf lint

## proto-breaking: fail if the vendored contract breaks wire compatibility vs the upstream foundation contract
proto-breaking:
	cd "$(PROTO_DIR)" && buf breaking --against "$(UPSTREAM_PROTO)"

## proto-sync: refresh the vendored .proto from the upstream foundation contract
proto-sync:
	cp "$(UPSTREAM_PROTO)/loop/foundation/source/v1/source_ingest.proto" \
		"$(PROTO_DIR)/loop/foundation/source/v1/source_ingest.proto"
