.PHONY: proto web server dev test

proto:  ## Regenerate Go + TS code from proto
	PATH="$(shell go env GOPATH)/bin:$$PATH" protoc \
		--proto_path=proto \
		--go_out=. --go_opt=module=pwa-homelab-mon \
		--connect-go_out=. --connect-go_opt=module=pwa-homelab-mon \
		--plugin=protoc-gen-es=web/node_modules/.bin/protoc-gen-es \
		--es_out=web/src/gen --es_opt=target=ts \
		proto/homelab/v1/homelab.proto

web:  ## Build frontend into web/dist
	cd web && pnpm install --frozen-lockfile && pnpm build

server:  ## Build Go binary
	go build -o bin/server ./server

dev:  ## Dev: Go server on :8080 + Vite on :5173 (use http://localhost:5173)
	go run ./server & cd web && pnpm dev

test:  ## Smoke-test the RPC endpoints
	@go build -o bin/server ./server
	@./bin/server & sleep 1; \
	for i in 1 2; do \
		curl -sf -X POST localhost:8080/homelab.v1.HomelabService/IncrementCounter -H 'Content-Type: application/json' -d '{}' | grep -q "\"value\":\"$$i\"" || exit 1; \
	done; \
	curl -sf -X POST localhost:8080/homelab.v1.HomelabService/SayHello -H 'Content-Type: application/json' -d '{"name":"Pony"}' | grep -q 'Hello, Pony!' || exit 1; \
	curl -sf -X POST localhost:8080/homelab.v1.HomelabService/GetLuckyNumber -H 'Content-Type: application/json' -d '{}' | grep -q '"number"' || exit 1; \
	kill %1; echo OK
