.PHONY: all build build-local build-edge build-edge-host build-node build-node-target build-node-targets pack-node-target pack-edge archive-edge tidy test test-e2e test-control-plane-edge-wire test-openai-ollama test-openai-lemonade readability-audit proto proto-dart client-test client-build-web clean

GOFLAGS ?= -trimpath
BUILD_DIR ?= build
BUILD_BIN_DIR ?= $(BUILD_DIR)/bin
ARTIFACT_DIR ?= $(BUILD_DIR)/artifacts
PACK_DIR ?= $(BUILD_DIR)/packages
EDGE_TARGET ?= $(shell go env GOOS)-$(shell go env GOARCH)
EDGE_TARGET_PARTS = $(subst -, ,$(EDGE_TARGET))
EDGE_GOOS = $(word 1,$(EDGE_TARGET_PARTS))
EDGE_GOARCH = $(word 2,$(EDGE_TARGET_PARTS))
EDGE_PACKAGE_NAME ?= iop-edge-$(EDGE_TARGET)
EDGE_PACKAGE ?= $(PACK_DIR)/$(EDGE_PACKAGE_NAME).tar.gz
EDGE_PACKAGE_TMP ?= $(BUILD_DIR)/.edge-package
EDGE_HOST_BINARY ?= $(BUILD_BIN_DIR)/iop-edge-host
NODE_TARGET ?= $(shell go env GOOS)-$(shell go env GOARCH)
NODE_TARGETS ?= linux-arm64 linux-amd64 darwin-arm64 darwin-amd64 windows-arm64 windows-amd64
NODE_TARGET_PARTS = $(subst -, ,$(NODE_TARGET))
NODE_GOOS = $(word 1,$(NODE_TARGET_PARTS))
NODE_GOARCH = $(word 2,$(NODE_TARGET_PARTS))
IOP_CONTROL_PLANE_HTTP_URL ?= http://localhost:18000
IOP_CONTROL_PLANE_WIRE_URL ?= ws://localhost:19080/client

all: build

build: build-node-targets
	$(MAKE) build-edge
	$(MAKE) archive-edge

build-local: build-edge build-node

build-edge:
	@test -n "$(EDGE_GOOS)" && test -n "$(EDGE_GOARCH)" || (echo "EDGE_TARGET must be <goos>-<goarch>" >&2; exit 2)
	mkdir -p $(BUILD_BIN_DIR)
	GOOS=$(EDGE_GOOS) GOARCH=$(EDGE_GOARCH) go build $(GOFLAGS) -o $(BUILD_BIN_DIR)/iop-edge ./apps/edge/cmd/edge

build-edge-host:
	mkdir -p $(BUILD_BIN_DIR)
	go build $(GOFLAGS) -o $(EDGE_HOST_BINARY) ./apps/edge/cmd/edge

build-node:
	mkdir -p $(BUILD_BIN_DIR)
	go build $(GOFLAGS) -o $(BUILD_BIN_DIR)/iop-node ./apps/node/cmd/node

build-node-target:
	@test -n "$(NODE_GOOS)" && test -n "$(NODE_GOARCH)" || (echo "NODE_TARGET must be <goos>-<goarch>" >&2; exit 2)
	mkdir -p $(BUILD_BIN_DIR)
	GOOS=$(NODE_GOOS) GOARCH=$(NODE_GOARCH) go build $(GOFLAGS) -o $(BUILD_BIN_DIR)/iop-node-$(NODE_TARGET) ./apps/node/cmd/node

build-node-targets: build-edge-host
	rm -rf "$(ARTIFACT_DIR)"
	@for target in $(NODE_TARGETS); do \
		echo "==> node target $$target"; \
		$(MAKE) build-node-target NODE_TARGET=$$target; \
		$(EDGE_HOST_BINARY) bootstrap pack --target $$target --node-binary $(BUILD_BIN_DIR)/iop-node-$$target --output $(ARTIFACT_DIR); \
	done

pack-node-target: build-edge-host build-node-target
	$(EDGE_HOST_BINARY) bootstrap pack --target $(NODE_TARGET) --node-binary $(BUILD_BIN_DIR)/iop-node-$(NODE_TARGET) --output $(ARTIFACT_DIR)

pack-edge: build-edge archive-edge

archive-edge:
	@test -d "$(ARTIFACT_DIR)/bootstrap" || (echo "missing node bootstrap artifacts; run make pack-node-target NODE_TARGET=<goos>-<goarch> first" >&2; exit 2)
	rm -rf "$(EDGE_PACKAGE_TMP)"
	mkdir -p "$(EDGE_PACKAGE_TMP)/$(EDGE_PACKAGE_NAME)/artifacts" "$(EDGE_PACKAGE_TMP)/$(EDGE_PACKAGE_NAME)/logs" "$(PACK_DIR)"
	cp "$(BUILD_BIN_DIR)/iop-edge" "$(EDGE_PACKAGE_TMP)/$(EDGE_PACKAGE_NAME)/iop-edge"
	cp -R "$(ARTIFACT_DIR)/." "$(EDGE_PACKAGE_TMP)/$(EDGE_PACKAGE_NAME)/artifacts/"
	tar -C "$(EDGE_PACKAGE_TMP)" -czf "$(EDGE_PACKAGE)" "$(EDGE_PACKAGE_NAME)"
	rm -rf "$(EDGE_PACKAGE_TMP)"
	@echo "wrote $(EDGE_PACKAGE)"

tidy:
	go mod tidy

test:
	go test ./...

readability-audit:
	python3 scripts/readability_audit.py --check --input-mode worktree --output build/readability-audit.json

test-e2e:
	@echo "NOTE: test-e2e runs auxiliary smoke (Edge-Node + OpenAI) plus Control Plane-Edge wire smoke; completion still requires user-flow verification when changing runtime paths."
	./scripts/e2e-smoke.sh
	./scripts/e2e-openai-ollama.sh
	./scripts/e2e-control-plane-edge-wire.sh

test-control-plane-edge-wire:
	./scripts/e2e-control-plane-edge-wire.sh

test-openai-ollama:
	./scripts/e2e-openai-ollama.sh

test-openai-lemonade:
	./scripts/e2e-openai-lemonade.sh

# Requires: protoc + protoc-gen-go (go install google.golang.org/protobuf/cmd/protoc-gen-go@latest)
proto:
	protoc \
		--go_out=. \
		--go_opt=module=iop \
		--proto_path=. \
		proto/iop/runtime.proto \
		proto/iop/node.proto \
		proto/iop/control.proto \
		proto/iop/job.proto

# Try finding protoc-gen-dart in PATH or common fallback locations
PROTOC_GEN_DART := $(shell which protoc-gen-dart 2>/dev/null)
ifeq ($(PROTOC_GEN_DART),)
  PROTOC_GEN_DART := $(wildcard $(HOME)/.pub-cache/bin/protoc-gen-dart)
endif
ifeq ($(PROTOC_GEN_DART),)
  PROTOC_GEN_DART := $(wildcard /config/.pub-cache/bin/protoc-gen-dart)
endif

proto-dart:
ifeq ($(PROTOC_GEN_DART),)
	@echo "Error: protoc-gen-dart not found."
	@echo "Please install it by running: flutter pub global activate protoc_plugin"
	@exit 1
endif
	mkdir -p apps/client/lib/gen
	protoc \
		--plugin=protoc-gen-dart=$(PROTOC_GEN_DART) \
		--dart_out=apps/client/lib/gen \
		--proto_path=. \
		--proto_path=/config/.local/include \
		proto/iop/runtime.proto \
		proto/iop/node.proto \
		proto/iop/control.proto \
		proto/iop/job.proto

client-test:
	cd apps/client && flutter test

client-build-web:
	cd apps/client && flutter build web \
		--dart-define=IOP_CONTROL_PLANE_HTTP_URL=$(IOP_CONTROL_PLANE_HTTP_URL) \
		--dart-define=IOP_CONTROL_PLANE_WIRE_URL=$(IOP_CONTROL_PLANE_WIRE_URL)

clean:
	rm -rf build
	rm -rf dist
	rm -f iop.db
	rm -f build/readability-audit.json
