.PHONY: proto build up down logs ps tidy gpu-preflight k8s-deploy k8s-bootstrap

SERVICES := iam billing conversation workspace knowledge agent notification gateway

# Regenerate every *.pb.go / *_grpc.pb.go from api/proto/**/*.proto.
# Requires: protoc, protoc-gen-go, protoc-gen-go-grpc on PATH
# (go install google.golang.org/protobuf/cmd/protoc-gen-go@latest &&
#  go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest).
proto:
	rm -rf gen
	mkdir -p gen
	protoc -I api/proto \
		--go_out=gen --go_opt=module=enkly/gen \
		--go-grpc_out=gen --go-grpc_opt=module=enkly/gen \
		api/proto/common/v1/*.proto \
		api/proto/iam/v1/*.proto \
		api/proto/billing/v1/*.proto \
		api/proto/conversation/v1/*.proto \
		api/proto/workspace/v1/*.proto \
		api/proto/knowledge/v1/*.proto \
		api/proto/agent/v1/*.proto

# Compile every service's binary locally (sanity check before building
# images) — requires normal internet access to resolve go.mod's
# dependencies, which this sandboxed authoring environment did not
# have; see ARCHITECTURE.md's "What's verified, what isn't".
build:
	@for s in $(SERVICES); do \
		echo "== $$s =="; \
		go build -o /dev/null ./services/$$s/cmd/server || exit 1; \
	done
	go build -o /dev/null ./tools/indexer/cmd/indexer

tidy:
	go mod tidy

up:
	# No separate model-pull step needed: vllm-chat/vllm-embed download
	# their own weights from Hugging Face on first start, and agent/
	# knowledge's depends_on: condition: service_healthy (see
	# docker-compose.yml) already blocks them from starting until both
	# vLLM servers' /health check passes — which, on a cold cache, can
	# take a few minutes for the Qwen3.5 chat model. `make logs` to
	# watch that progress.
	docker compose up --build -d

down:
	docker compose down

logs:
	docker compose logs -f

ps:
	docker compose ps

# Diagnoses why vllm-chat/vllm-embed are Pending on the k8s deployment
# (GPU capacity, node label/taint, PVC binding) — see k8s/README.md
# "GPU prerequisites" before running this against a real cluster.
gpu-preflight:
	./k8s/scripts/gpu-preflight.sh

# Apply + wait for rollout only — assumes the GPU node is already
# labeled/tainted and 02-secrets.yaml is already filled in. Normally
# called by bootstrap-cluster.sh (k8s-bootstrap, below); use this
# directly only when you've done the GPU setup yourself.
k8s-deploy:
	cd k8s && ./deploy.sh

# ONE command for the whole k8s/ manifest set: auto-detects + labels/
# taints the GPU node(s), applies time-slicing if needed, generates
# ENKLY_VLLM_API_KEY, applies everything (via k8s-deploy above), then
# runs gpu-preflight. See k8s/README.md "Deploy".
k8s-bootstrap:
	cd k8s && ./bootstrap-cluster.sh
